I just ran a test with your code and in my opinion, this is not working because you have an error in the get_callback
function.
First time it will try to get the subtitle
( or the timing_of_experience
) but it will trigger an error because it doesn’t exists in the first place and this error will block the registration of the update_callback
.
So, the problem is that in the get_callback
the Subtitle
key has a capital S
and this is not the way it is saved.
Second, you should respect the general rule of validating data and check if the value exists before trying to access it. Like this:
function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
$meta = get_post_meta( $post_id );
if ( isset( $meta['subtitle' ] ) && isset( $meta['subtitle' ][0] ) ) {
//return the post meta
return $meta['subtitle' ][0];
}
// meta not found
return false;
}
Like I said, I ran a test, replacing your experience
post type with post
and it is working.
Bonus tips, you should try to organize your code better since it is hard to read with this indentation and functions order. also, the for the update_callback
a simple return true
is enough.
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 );
} else {
$ret = add_post_meta( $object['id'], 'subtitle', $meta_value ,true );
}
return true;
}