0
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( 'post', 'post-meta-fields', array(
       'get_callback'    => 'get_post_meta_for_api',
       'schema'          => null,
    )
);

// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'achievement', 'post-meta-fields', 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 );}

Please the code above! Tia

  • 1
    Are you trying to add an endpoint to retrieve your posts or add meta to the results? You question and code are about 2 different things. – Jacob Peattie Mar 26 '18 at 9:54
  • Im trying to create an endpoint that will retrieve/show all posts in my custom post type. And I also want to filter it by showing the id and the title(rendered) only. – Ron Ogrimen Mar 26 '18 at 10:19
1

The "register_rest_field" function will add and handle data update for a given endpoint, looks like it's not what you are looking for.

The register_post_type() function accepts a "show_in_rest" and "rest_base" params if you want to use a custom slug for the endpoint:

https://codex.wordpress.org/Function_Reference/register_post_type

By default it uses the WP_REST_Posts_Controller class to handle the methods (get, update, delete content), this means that you can refer to the following documentation:

https://developer.wordpress.org/rest-api/reference/posts/

There are already a few parameters you can pass for filtering (https://developer.wordpress.org/rest-api/reference/posts/#list-posts), but if you want to alter the results/order returned on the GET method to suit your custom needs, you have to pass a custom class for the REST controller param("rest_controller_class") in the register_post_type() function.

  • Thank you for this. I just put ('show_in_rest' => true) on my register_post_type and it works. Thank you again. About filtering the response on my endpoint, I really cant find a solution where I can see only the id and the title of the post. Is there a way that i can use a request like /wp-json/wp/v2/posts?per_page=100 for filtering the id and title? – Ron Ogrimen Mar 27 '18 at 2:39
  • Hi @RonOgrimen, not sure I do understand your question but if the goal is to show only id and title within the REST API response for that endpoint, you should have to use a custom class controller, you can take the posts one as a base(WP_REST_Posts_Controller) and use it for your custom post type. I'd recommend to check the methods for that class if you want to return a custom response and custom sort. – Pabamato Mar 27 '18 at 15:14
  • Did you try the include param?: /wp-json/wp/v2/posts?per_page=100&include=1,2,3,4 also the search, order and order by can help: developer.wordpress.org/rest-api/reference/posts/#arguments – Pabamato Mar 27 '18 at 15:16
  • Thank you @pabamato, I really appreciate it. Right now I need only to show posts by the current date. Is there a request like that? ?search= is not working – Ron Ogrimen Mar 28 '18 at 1:56
  • Did you try using "after" and "before" parameters? You have to pass an ISO 8601 date string as parameter, you can use WordPress built in time function for time formatting: codex.wordpress.org/Formatting_Date_and_Time – Pabamato Mar 29 '18 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.