Error rest_post_invalid_page_number trying to call Rest API

I got the following error, trying to call all posts or alle posts from a specific post type via the REST API:

/wp-json/wp/v2/posts
/wp-json/wp/v2/posts?page=1
/wp-json/wp/v2/posts?page=1per_page=1
/wp-json/wp/v2/faq

The error which appears is:

{ "code": "rest_post_invalid_page_number", "message": "The page number requested is larger than the number of pages available.", "data": { "status": 400 } }

I got two posts within the custom post type and one normal post.

What causes this error?

Topic rest-api Wordpress

Category Web


I had the same above issue. I disabled the 'pre_get_posts and it worked fine.

// add_action('pre_get_posts','pre_queries');


I don't know why yet, but I had the same issue until I disabled a filter on pre_get_posts.

What I'm doing here is retrieve all recipe custom post type when I'm on the home page :

// add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query )
{
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'recipe' ) );
        $query->set( 'posts_per_page', -1 );

    return $query;
}

Try to know if you're hijacking a main query like I did, I'll do more research on the "why" :)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.