I am doing an IONIC app and the project requires to create a post through the Wordpress API with custom fields (I am using ACF Pro and Aires ACF to REST API), I can create a normal post with the REST API, even tho, the ACF fields are not created or asiggned, they are created for the post only if I create the post in Wordpress admin interface.
I was reading the documentation for the ACF to REST API , but I see only edit and show capabilities. Does that mean the post meta have to exist? So how to "create" them for the post
Thanks in advance for the help
Best regards
phpadmin post creating:
absence of post meta:
$json = file_get_contents('PATH/TO/FILE.json');
function the_json_contents($json_to_get){
$json_in_array = json_decode($json_to_get, true); //json string to array
return $json_in_array;
}
function do_stuff_do_json() {
$json_array = the_json_contents($json);
foreach($json_array['id'] as $js) { //assuming we have an 'id' key
$somevariable = $js['someKey'];
$somevariable2 = $js['someOtherKey'];
}
}
Refer this solution
Related
I have a custom field in my product's page that is saved to database and it is also visible in my editor
I am trying to access it through API as a meta data and I am successful in updating that field.
The problem is that it doesn't automatically gets updated on my frontend and I have to click update (product) to get updated value in woocommerce.
I am using this code to save it to my wp database
function save_woocommerce_product_custom_fields($post_id)
{
$product = wc_get_product($post_id);
$custom_fields_woocommerce_title = isset($_POST['alt_points']) ? $_POST['alt_points'] : '';
$product->update_meta_data('alt_points', sanitize_text_field($custom_fields_woocommerce_title));
$product->save();
}
add_action('woocommerce_process_product_meta', 'save_woocommerce_product_custom_fields');
now I need this code as an api to automatically show it on frontend
I would like to create post titles automatically. The post title is made from the category and category ID before the post is created in the database but does not get the category and ID.
Any help will be much appreciated, Thank You.
function my_post_titles($data, $postarr){
$postid = $postarr['ID'];
$postcategory = $postarr['post_category'];
$data['post_title'] = $postcategory."-".$postid;
return $data;
}
add_filter('wp_insert_post_data', 'my_post_titles', '99', 2 );
Couple of things to be aware of here: wp_insert_post_data is the filter used by wp_insert_post to allow changes to its first parameter, so to understand your problem, you should check how wp_insert_post works.
https://developer.wordpress.org/reference/functions/wp_insert_post/
Essentially, if the post you are creating a new post, then it should not have any ID (as it's not even created and saved to the database yet). To have access to the post ID, I suggest you use save_post hook rather than wp_insert_post. save_post is the action triggered after the post is created or updated. For example:
add_action('save_post', function($post_id) {
$title = get_the_title($post_id);
if ($title) {
return; // if the post has already has a title, then do nothing
}
// Otherwise, update the post title here
});
Also, $postarr['post_category'] is an array, not a string, so to get the correct information, you must convert it to string before concat it with the post_id.
I am currently using WordPress with Laravel. I write posts in WordPress and display the posts in my Laravel application using WordPress REST API and GuzzleHttp.
Is there any way I can use different filters like where(), sortBy() etc. for filtering the data received from WordPress REST API using GuzzleHttp?
Yes you can serialize the json response to a Laravel Collection and use functions like that from there.
Retrieve the response body, which we assume contains json.
$response = $response->getBody();
Turn the json into an array. Note the seconds parameter is set to true to always output an array.
$responseArray - json_decode($response, true);
Turn the array into a Laravel collection in order to use its cool functions.
$collection = collect($responseArray);
So in short:
$collection = collect( json_decode( $response->getBody(), true ) );
$sortedByPrice = $collection->sortBy('price');
I used Custom Field Suite to add fields to all WordPress posts, but the field data does not show when I retrieve post data through the WP Rest API using /posts on a stand alone site.
Is there a way to add the CFS data so that it is retrieved by wp-json/posts REST calls? It feels like I just need to put a CFS()->get call in the right place. Or just a basic way to add data to posts so they are retrievable in REST calls?
I've added github issues for CFS and WP-API, but any help or direction would be appreciated. Thanks.
If someone use WP REST API V2, this may help:
adding this to function.php adds CFS for custom post type and standard posts (project in my case)
function cfs_to_json_api($post_response, $post, $context) {
$post_response->data['CFS']=CFS()->get( false, $post->ID, array( 'format' => 'raw' ) );
return $post_response ;
}
add_filter('rest_prepare_project', 'cfs_to_json_api', 12, 3);
add_filter('rest_prepare_post', 'cfs_to_json_api', 12, 3);
IMPORTANT:
True:
add_filter ('rest_prepare_post' 'function', 12, 3);
False:
add_filter ('json_prepare_post' 'function', 12, 3); //WRONG
If is Custom Post Type:
add_filter ('rest_prepare_{$post_type}' 'function', 12, 3);
EXAMPLE: Name post type = product;
add_filter ('rest_prepare_product' 'function', 12, 3);
Most easiest way to add custom fields in the WP Rest API is to used Advance Custom field plugin, with following small plugin extension.
Add Advance Custom Fields to JSON API
I've made a wordpress video section for one of my clients. In the backend it's a custom post type where the users can place the ID (http://www.youtube.com/watch?v=kFHshctPO9E) of a video.
With this ID i embed the video and get the duration and rating of each video from the youtube api.
Now each time a video is loaded (in the overview template all the video's are loaded) it has to reach out to YouTube to get the duration (which i then parse to hours and minutes instead of seconds) and rating of all video's.
I would somehow like to cache or save these values to a custom field or somewhat.
Anyone got any idea?
Thank in advance!
Rick
You can use post_meta to save this information the first time the post is saved and then load the information from the database instead of using youtube api again.
I put you an example in pseudocode:
add_action('save_post', 'my_function_to_save_posts_action');
function my_function_to_save_posts_action($post_id) {
$the_post = get_post($post_id);
if ($the_post->type == 'my_custom_post_type') {
$youtube_data = my_function_to_retrieve_youtube_data();
if (my_function_exists_post_meta($post_id)) {
update_post_meta($post_id, 'my_video_duration', $youtube_data['duration']);
update_post_meta($post_id, 'my_video_rating', $youtube_data['rating']);
}
else {
add_post_meta($post_id, 'my_video_duration', $youtube_data['duration'], true);
add_post_meta($post_id, 'my_video_rating', $youtube_data['rating'], true);
}
}
}
And of course, when you are queryng for your custom post type you must retrive the post_meta information and show this information properly.