[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
Wed Sep 13 05:24:46 UTC 2017


#41866: REST API throws error when displaying an unlimited number of posts in a
request
-------------------------------------------------+-------------------------
 Reporter:  dpsjfveloso1                         |      Owner:
     Type:  defect (bug)                         |     Status:  new
 Priority:  normal                               |  Milestone:  Awaiting
Component:  REST API                             |  Review
 Severity:  normal                               |    Version:  trunk
  Focuses:  administration, template,            |   Keywords:
  multisite, rest-api, performance               |
-------------------------------------------------+-------------------------
 Hi there,

 The REST API posts/pages endpoints fails when using a filter to set the
 ''posts_per_page'' as ''-1'', it throws a WP_Error,
 rest_post_invalid_page_number, as the number of max pages, ''$max_pages'',
 is negative as the ''posts_per_page'' has a negative value (-1).

 The error can be reproduced by applying a filter for the
 ''rest_page_query'' filter, setting the posts_per_page argument as -1:

 {{{#!php
 <?php
 function remove_posts_limits( array $arguments = array(), \WP_REST_Request
 $restApiRequest ) {
     // Let's suppose that we want to remove the number of posts per page
 as unlimited under certain conditions, and this is the reason why we use
 this filter
     $arguments[ 'posts_per_page' ] = -1;

     return $arguments;
 }

 add_filter( 'rest_page_query', 'remove_posts_limits', 10, 2);
 }}}

 The issue is caused due to the way that ''$max_pages'' is set (file /wp-
 includes/rest-api/endpoints/class-wp-rest-posts-controller.php, starts at
 line 326):

 {{{#!php
 <?php
 /*
 .
 .
 .
 */
 $max_pages = ceil( $total_posts / (int)
 $posts_query->query_vars['posts_per_page'] );

                 if ( $page > $max_pages && $total_posts > 0 ) {
                         return new WP_Error(
 'rest_post_invalid_page_number', __( 'The page number requested is larger
 than the number of pages available.' ), array( 'status' => 400 ) );
                 }

 /*
 .
 .
 .
 */
 }}}

 The simplest solution here would be to use the ''abs()'' after casting
 ''posts_per_page'' value:

 {{{#!php
 <?php
 /*
 .
 .
 .
 */

 $max_pages = ceil( $total_posts / abs( (int)
 $posts_query->query_vars['posts_per_page'] ) );

 /*
 .
 .
 .
 */
 }}}

 But the ''$max_pages'' could also be set inside of an if clause, as the
 value of $max_pages should be one in all cases (this would be a better
 solution):

 {{{#!php
 <?php
 /*
 .
 .
 .
 */

 $posts_per_page = (int) $posts_query->query_vars['posts_per_page'];

 $max_pages = ceil( $total_posts / $posts_per_page );

 if ($posts_per_page < 0) {
     // It's an unlimited number of posts per page, which means that
 $max_pages must be 1
     $max_pages = 1;
 }

 /*
 .
 .
 .
 */

 }}}


 Thanks,
 Jorge

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


More information about the wp-trac mailing list