I am trying to create a REST APIs for my wordpress website which is used for facility listing using wordpress job manager plugin.
I have registered my custom post , taxonomies in pluginsrest-apiplugin.php.
below API gives me all the listings with default response.
http://localhost/sports/wp-json/wp/v2/joblisting/
I wanted to add post meta in the JSON response using the below code.
function slug_register_phone_number() {
register_rest_field( 'job_listing',
'phone',
array(
'get_callback' => 'slug_get_phone_number',
'update_callback' => null,
'schema' => null,
)
);
}
function slug_get_phone_number($post, $field_name, $request) {
return get_post_meta($post->id, '_phone' );
}
}
Using above code i am able to add “phone” as a REST response but i am always getting phone = false in response. It is not showing the correct data from wp_postmeta table.
I have followed below mentioned links for reference.
http://v2.wp-api.org/extending/modifying/
Plug in details.
1. WP Job manager
2. rest-api
Any help will be really helpful.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
$post in the callback function is an array, not an object. So you cannot use $post->id. Change it to $post['id'] and it should work:
function slug_get_phone_number($post, $field_name, $request)
{
return get_post_meta($post['id'], '_phone', true);
}
I recommend to change _phone to phone_number or something else without underscore prefix. Because _ is often used with private meta keys. Try to add a custom field which has meta key with _ prefix directly to your post, you will see what I meant.
Method 2
WP API has a rest_prepare_post filter (or rest_prepare_CPT if you are working with custom posts) which you can use to modify the JSON response.
In your case it will be rest_prepare_joblisting.
function filter_joblisting_json( $data, $post, $context ) {
$phone = get_post_meta( $post->ID, '_phone', true );
if( $phone ) {
$data->data['phone'] = $phone;
}
return $data;
}
add_filter( 'rest_prepare_joblisting', 'filter_joblisting_json', 10, 3 );
Using the same filter you can also remove fields/data from the response and do any manipulation of the data.
Method 3
Just Add this methods to 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( 'tour', 'metaval', array(
'get_callback' => 'get_post_meta_for_api',
'schema' => null,
)
);
}
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 );
}
Method 4
Here is an OOP example:
class MetaDataFetcher{
public function enableAPIroute(){
add_action ('rest_api_init', array($this, 'doRegisterRoutes'));
}
public function doRegisterRoutes(){
register_rest_route(
'yournamespace/vXX',
'fetch-post-meta',
array(
'methods' => array('GET','POST'),
'callback' => array($this, 'returnMetaData'),
//You should have a better auth, or this endpoint will be exposed
permission_callback' => function(){return TRUE;}
);
}
public function returnMetaData(){
if (!(isset($_REQUEST['post-id']))){
return "ERROR: No post ID";
}
$postID = $_REQUEST['post-id'];
$meta = get_post_meta($postID);
$meta = json_encode($meta);
return $meta;
}
}
$MetaDetaFetcher = New MetaDataFetcher;
$MetaDetaFetcher->enableAPIroute();
Method 5
I tested and used what seems to be an easier way to me,
after you register the custom post type you can also register the custom meta and make it available in the rest API.
See register_meta()
register_post_type( 'job_listing', $args ); // in $args you can make the post type available in rest api
register_meta(
'post', '_phone', [
'object_subtype' => 'job_listing',
'show_in_rest' => true
]
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0