[wp-trac] [WordPress Trac] #41866: REST API throws error when displaying an unlimited number of posts in a request

WordPress Trac noreply at wordpress.org
Sat Nov 19 19:39:57 UTC 2022


#41866: REST API throws error when displaying an unlimited number of posts in a
request
--------------------------+-----------------------
 Reporter:  dpsjfveloso1  |       Owner:  (none)
     Type:  defect (bug)  |      Status:  closed
 Priority:  normal        |   Milestone:
Component:  REST API      |     Version:  4.9
 Severity:  normal        |  Resolution:  invalid
 Keywords:                |     Focuses:  rest-api
--------------------------+-----------------------

Comment (by onetarek):

 **I am getting the same problem on WP version 6.1.1.**

 Using the REST URL '/wp-json/wp/v2/events/' I am fetching all events
 posts. My code is modifying the wp-query object to set posts_per_page = -1
 via pre_get_posts hook.

 {{{#!php
 <?php
 add_action( 'pre_get_posts', array($this, 'qw_pre_get_posts') );

 function qw_pre_get_posts( $query ){

         if( !is_admin() && !is_home() && !is_page()){

                 $meta_query = array(
       'relation' => 'OR',
       array(
         'key' => 'sticky',
         'value' => array(1, '1', true, 0, '0', false, 'on')
       ),
       array(
         'key' => '_qw_events_date',
       )
     );

     $query->set( 'meta_query', $meta_query );
     $query->set( 'orderby', 'meta_value' );
     $query->set('meta_key', '_qw_events_date');
     $query->set('order', 'ASC');
     $query->set('posts_per_page', -1);

    }
 }
 }}}



 **REST RESPONSE:**

 {{{
 [{"id":1468,"date":"2022-07-29T13:14:37","date_gmt":"2022-07-29T17:14:37","guid":{"rendered":"http:\/\/example.com\/?post_type=events&p=1468"},"modified":"2022-11-14T09:58:50","modified_gmt":"2022-11-14T13:58:50","slug
 ":"greensboro-symphony-with-michael-
 feinstein","type":"events","link":"https:\/\/example.com\/events......
 }}}


 My code was working perfectly on WP **version 4.6.25**. After updating to
 **version 6.1.1** I am getting the following response from REST API.

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

 If I disable the line '$query->set('posts_per_page', -1);' REST API
 returns event post data.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/41866#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list