Add Post Meta Fields via WP API v2 - wordpress

I'm trying to play with the WP API v2 and insert posts from Postman.
If I post this raw request, it creates a post just fine:
{
"title": "Test Title",
"content": "Test Content",
}
However, I'm trying to add some custom field values to this as well, and I can't seem to get them to work. This request creates a post, but doesn't add any meta fields:
{
"title": "Test Title",
"content": "Test Content",
"meta": {
"foo": "bar",
"foo2": "bar2"
}
}
How do I POST the meta fields foo and foo2 with the values bar and bar2 through the API endpoint https://my-site.com/wp-json/wp/v2/posts?
Edit: It also appears custom fields don't get pulled natively in GET requests. I put this code in a mu-plugin:
add_filter( 'rest_prepare_post', 'xhynk_api_post_meta', 10, 3 );
function xhynk_api_post_meta( $data, $post, $context ){
$meta = get_post_custom( $post->ID );
if( $meta ) {
$data->data['meta'] = $meta;
}
return $data;
}
Which at least lets me view it on a GET request. However I still can't seem to get it to POST via Postman. Even adding "status": "publish" will cause the new post to publish instead of being a draft like it is by default. Are there any hooks or filters I can use on API POST requests to make sure the custom fields are added?

to handle metas on insertion and update, you can do it with action rest_insert_ + post type
add_action("rest_insert_page", function (\WP_Post $post, $request, $creating) {
$metas = $request->get_param("meta");
if (is_array($metas)) {
foreach ($metas as $name => $value) {
update_post_meta($post->ID, $name, $value);
}
}
}, 10, 3);

Related

How to display post and custom post type count by published by author in wordpress

I need to display the total post count published by the author this code is displayed the total number of post
Screenshot
Please Check both the code and screenshot and give me a solution on how can I display the total number of posts by the author.
// Post Type - Post
function postcode( $atts )
{
return (count_user_posts(get_the_author_meta('ID'),'post') );
}
add_shortcode( 'codepost', 'postcode');
// Custom Post Type
function tourcode( $atts )
{
return (count_user_posts(get_the_author_meta('ID'),'tourism') );
}
add_shortcode( 'codetour', 'tourcode');
Easy just put all the post_types into an array like below:
// All post types
function my_get_author_post_count( $atts )
{
return count_user_posts(get_the_author_meta('ID'), ['post', 'tourism']);
}
add_shortcode( 'get_author_post_count', 'my_get_author_post_count');

Is there any method to generate auto title inside wordpress post when publishing

I have a custom post type where i can create post and publish.The title
can be any random string.
Is there any method to auto modify post title when a post is published?
If you want to set a title when a post is created on the back end, you have to use the title_save_pre filter. It works on creating a new post or updating an existing post without change.
add_filter( 'wp_insert_post_data' , 'modify_your_post_title' , '99', 1 );
function modify_your_post_title( $data )
{
if($data['post_type'] == 'rating') { /
$id = get_the_ID();
$title = 'Post Title for ' . $id;
$data['post_title'] = $title ; //Updates the post title to your new title.
}
return $data; // Returns the modified data.
}

Add Heartbeat API's filter in plugin - Wordpress

I would like to receive a custom value from a request made by WordPress Heartbeat API.
The problem is that the filter only work in functions.php and not from plugin constructor as I always do.
I have plenty others hooks which perfectly work. The syntax is correct. Can I misunderstand the loading sequence ?
In plugin constructor (don't work):
add_filter( 'heartbeat_received', array( $this, 'test_heartbeat_received' ), 10, 2 );
function test_heartbeat_received( $response, $data ) {
$response['test'] = 'test';
return $response;
}
In functions.php (work):
add_filter( 'heartbeat_received', 'test_heartbeat_received', 10, 2 );
function test_heartbeat_received( $response, $data ) {
$response['test'] = 'test';
return $response;
}
I expect this JSON response : { test: "test", "wp-auth-check": true, server_time: 1546856046 },
but only get { "wp-auth-check": true, server_time: 1546856046 }.
Thank you for your help !
EDIT
I have managed to make it work by putting the filter outside the main class, the problem could be in relation with POO but I don't understand the process...
add_filter( 'heartbeat_received', array( new Test, 'test_heartbeat_received' ), 10, 2 );
My class, related to front page, was instantiated with the conditionnal tag !is_admin() so all AJAX actions were not triggered. Without this condition it works !

WP REST-API create posts with custom fields generated by CPT

I used the CPT to create a post type UserQuestion with a few fields, such as ip_data. I want to be able to create one of this posts through API. So I looked into WP REST API .
However, the API offers /v2/user_question:
{
"title" : "test2",
"slug": "user_question",
"status": "publish",
"post_type": "user_question",
"meta": {
"ip" : "1111",
"question": "test question",
"answer": "yes, the answer"
}
}
The post is created, but it's not updating the customized fields data.
How should I make the request?
add_action("rest_insert_user_question", function (\WP_Post $post, $request, $creating)
{
$metas = $request->get_param("meta");
if (is_array($metas)) {
foreach ($metas as $name => $value) {
//update_post_meta($post->ID, $name, $value);
update_field($name, $value, $post->ID);
}
}
}, 10, 3);
In your functions.php (or in your plugin) add the above add_action and function. Change the 'user_question' in the add_action to match your post type, for example "rest_insert_portfolio" for a portfolio post type. Use update_field if you're using Advanced Custom Fields or update_post_meta if you're using regular custom fields.

How add a new property to Post entity in "WP REST API" plugin of WordPress?

I'm trying to add a new property to the Post entity of "WP REST API" plugin. The new property is "views". It means the numbers of times that a post has been viewed. The data come from the plugin "Post Views Counter" how is on the WordPress table wp_post_views.
What's is the easiest form to do it?
Thanks in advance,
This is a very simple answer, using a wordpress plugin:
function get_viewed_count_post( $data, $post, $context ) {
// We only want to modify the 'view' context, for reading posts
if ( $context !== 'view' || is_wp_error( $data ) ) {
return $data;
}
//Eliminate unused variables
unset($data['author']);
unset($data['terms']);
unset($data['meta']);
$viewed = db_select($data['ID']);
if (!empty( $viewed )) {
$data['post_viewed'] = $viewed;
}
return $data;
}
function db_select($id) {
global $wpdb;
$px_table_name = $wpdb->prefix.'post_views';
$sql = "SELECT count FROM $px_table_name WHERE ID='$id' AND type = 4";
return intval($wpdb->get_var($sql));
}
add_filter( 'json_prepare_post', 'get_viewed_count_post', 13, 3 );

Resources