I am trying to add custom post data( post meta data) through wordpress API but I am getting difficulty while updating/adding custom post data. below is the code that am using.
Code written in function.php
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'experience', 'subtitle', array(
'get_callback' => 'get_post_meta_for_api',
'update_callback' => 'update_post_meta_for_exp',
'schema' => null,
)
);
}
function update_post_meta_for_exp($object, $meta_value ) {
$havemetafield = get_post_meta($object['id'], 'experience', false);
if ($havemetafield) {
$ret = update_post_meta($object['id'], 'subtitle', $meta_value );
return true;
} else {
$ret = add_post_meta( $object['id'], 'subtitle', $meta_value ,true );
return true;
}
}
function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
return get_post_meta( $post_id )["Subtitle"][0];
}
function create_api_posts_meta_field_time() {
// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'experience', 'timing_of_experience', array(
'get_callback' => 'get_post_meta_for_api_time',
'update_callback' => function($meta_value ) {
$havemetafield = get_post_meta(1, 'experience', false);
if ($havemetafield) {
$ret = update_post_meta(1, 'timing_of_experience', $meta_value );
return true;
} else {
$ret = add_post_meta( 1, 'timing_of_experience', $meta_value ,true );
return true;
}
},
'schema' => null,
)
);
}
function get_post_meta_for_api_time( $object ) {
//get the id of the post object array
$post_id = $object['id'];
//return the post meta
return get_post_meta( $post_id )["timing_of_experience"][0];
}
JS file included in page I am working on
var quickAddExperience = document.querySelector("#quick-add-experience");
if (quickAddExperience) {
quickAddExperience.addEventListener("click",function() {
var ourPostData = {
'title' : document.getElementById('title').value,
'content' : document.getElementById('content').value,
'subtitle' : document.getElementById('company_name').value,
'timing_of_experience' : document.getElementById('time_period').value,
'status' : 'publish'
}
console.log(ourPostData);
var createPost = new XMLHttpRequest();
createPost.open("POST", magicalData.siteURL + "/wp-json/wp/v2/experience-api");
createPost.setRequestHeader("X-WP-Nonce", magicalData.nonce);
createPost.setRequestHeader("Content-Type","application/json;charset=UTF-8");
createPost.send(JSON.stringify(ourPostData));
createPost.onreadystatechange = function(){
if (createPost.readystate == 4) {
if (createPost.status == 201) {
document.querySelector('.data-api-post-1 [name="title"]').value ='';
document.querySelector('.data-api-post-1 [name="content"]').value ='';
document.querySelector('.data-api-post-1 [name="company_name"]').value ='';
document.querySelector('.data-api-post-1 [name="time_period"]').value ='';
}else{
alert("Error - try again");
}
}
}
});
}
This code is working for default field means new post is created but only title and content is there.
Edit: When I tried to debug the then came to know that update_callback code is not executing but get_callback is executing.