admin管理员组

文章数量:1122846

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=1&per_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?

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=1&per_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?

Share Improve this question edited Dec 5, 2018 at 22:46 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Dec 4, 2018 at 9:55 MaxMax 1763 silver badges15 bronze badges 6
  • What's your code? And you should use per_page to limit the number of posts, like so: /wp-json/wp/v2/posts?per_page=3 - demo. That's for the standard post type, but should also work for custom post types. – Sally CJ Commented Dec 4, 2018 at 10:39
  • I have no code written yet. I am just trying to get the result within the browser. – Max Commented Dec 4, 2018 at 10:55
  • Ok, but the error actually clearly suggests that there were no posts found for the request. Are you 100% sure the normal post you have is not private (and published)? If yes, then there's probably a plugin or custom code which filters the REST API response and excluding that post. – Sally CJ Commented Dec 4, 2018 at 11:26
  • I am absolutely sure that there are published posts. :/ – Max Commented Dec 4, 2018 at 11:27
  • The only plugin activated is AdvancedCustomFieldsPro – Max Commented Dec 4, 2018 at 11:29
 |  Show 1 more comment

2 Answers 2

Reset to default 0

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" :)

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

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

本文标签: Error restpostinvalidpagenumber trying to call Rest API