I get the custom meta field with rest api but not able to create or update.
I’m not a programmer, I tried different solutions found on various sites but it does not work any . I used postman, the new post creatation is ok but custom meta fields not created.
Example:
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( 'job_listing', 'post-meta-fields', array(
'get_callback' => 'get_post_meta_for_api',
'update_callback' => 'update_post_meta_for_api',
'schema' => null,
)
);
}
function get_post_meta_for_api( $object, $meta_value ) {
//get the id of the post object array
$post_id = $object['id'];
$meta = get_post_meta( $post_id );
if ( isset( $meta['_date-start' ] ) && isset( $meta['_date-start' ][0] ) ) {
//return the post meta
return $meta['_date-start' ][0];
} else {
update_post_meta( $meta, '_date-start', $meta_value ,true );
}
// meta not found
return false;
}