I have this code in my wordpress plugin. I'm working on an vue powered headless theme and I need to get all the informations about pages using the rest API.

I've registered with success a custom res field for a cpt, but now when I try to add a rest field for page object, it will be not added if I call the wp-json\wp\v2\pages I will not see the added field

function __construct()
  add_action('rest_api_init', [$this, 'setup_custom_routes']);

function setup_custom_routes(){

                'get_callback' => [$this, 'get_pages_cover']

function get_pages_cover( $post ){
    return get_the_post_thumbnail_url( $post['id'] );

Is there something wrong, or I need to do this in another way?

  • Is this your actual code? You’re I’m using $this but your code doesn’t actually seem to be in a class. Feb 21 at 11:18
  • I've pasted the code from my class, I'm in an OOP context. I've edited the post to match my code
    – ICTDEV
    Feb 21 at 11:34
  • Does your actual code include the space as in setup_custom routes?
    – Sally CJ
    Feb 21 at 12:25
  • no, it's a typo I've made during the post writing, in my code there is no space into the callback name
    – ICTDEV
    Feb 21 at 12:46
  • In that case, then your code is good. So how do you instantiate the class? When and where - from within a hook, or the root in your main plugin file? Are you sure page_cover is not in the response JSON, or was it there, but empty (no value)? How did you confirm the field was not there, how do you "call wp-json/wp/v2/pages"?
    – Sally CJ
    Feb 21 at 13:05

1 Answer 1


You may need to flush the REST API cache in order to see the added field. You can do this by adding the following code to your plugin:

This code will flush the REST API cache whenever a post (including a page) is saved or updated, so that the changes to the registered field will appear immediately.

If the field still does not appear after flushing the cache, you may want to check that the REST API request you're making is for a single page object and not a collection. The field will only be returned when you make a GET request for a single page, for example: wp-json/wp/v2/pages/123.

function flush_rest_api_cache() {
add_action( 'save_post', 'flush_rest_api_cache' );
  • Thank you. This solved the problem and now I have my field in the rest api response. The only strange thing is that the filter to modify the rest default address will be ignored from the system, but this is another problem
    – ICTDEV
    Feb 21 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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