[wp-trac] [WordPress Trac] #24142: Zero value for posts_per_page value in wp_query custom instance and for 'Blog pages show at most' option

WordPress Trac noreply at wordpress.org
Fri May 26 04:32:13 UTC 2023


#24142: Zero value for posts_per_page value in wp_query custom instance and for
'Blog pages show at most' option
-------------------------------------------------+-------------------------
 Reporter:  alexvorn2                            |       Owner:
                                                 |  SergeyBiryukov
     Type:  defect (bug)                         |      Status:  reviewing
 Priority:  normal                               |   Milestone:  6.3
Component:  Query                                |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  needs-testing early needs-testing-   |     Focuses:
  info                                           |
-------------------------------------------------+-------------------------

Comment (by Howdy_McGee):

 @audrasjb The goal of this patch is to make WP_Query's `pre_get_posts`
 more predictable.

 **Prior** to this patch:

 Passing `true` would show all posts, but the WP_Query object would only
 show `posts_per_page => 1`

 Passing `0` would show the default of 10 posts and update the WP_Query
 property `posts_per_page => 10`

 **Post**-patch fixes these inconsistencies.

 Passing `true` will interpret the boolean as 1 and only show 1 post.
 `posts_per_page => 1`

 Passing `0` will query no posts. `posts_per_page => 0`

 For a wider view of how the `pre_get_posts` argument gets handled, you can
 view [https://core.trac.wordpress.org/ticket/24142#comment:10 the table
 above].

 The added Unit Tests also cover an area previously untested for WP_Query.

 -----

 One way to test this patch is to run a `pre_get_posts` against X blog
 posts.

 {{{#!php
 <?php
 add_action( 'pre_get_posts', function( $query ) {

         if( $query->is_main_query() ) {
                 $query->set( 'posts_per_page', 0 );     // Shows 0 posts
                 $query->set( 'posts_per_page', true );  // Shows 1 post
 per page
         }

 } );
 }}}

 -----

 While I do think this is an update worth making, it may cause initial
 confusion for developers where their code already gets a dynamic value for
 `pre_get_posts`. For example:


 {{{
 'pre_get_posts' => get_option( 'nonexistent_pre_get_posts_key' )
 }}}


 Where `get_option()` (or however a developer may be getting their value)
 returns false, the system interprets it as 0 and returns no posts. With
 that being said, spitting out WP_Query will reflect `pre_get_posts` as 0,
 denoting no posts to show, which is technically correct.

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


More information about the wp-trac mailing list