[wp-trac] [WordPress Trac] #18897: query_posts / WP_query parameter 'offset' should play nicely with 'paged'

WordPress Trac noreply at wordpress.org
Wed Nov 27 00:23:23 UTC 2013


#18897: query_posts / WP_query parameter 'offset' should play nicely with 'paged'
------------------------------+-----------------------------
 Reporter:  here              |       Owner:  wonderboymusic
     Type:  enhancement       |      Status:  reopened
 Priority:  normal            |   Milestone:  3.8
Component:  Query             |     Version:  3.2.1
 Severity:  normal            |  Resolution:
 Keywords:  has-patch commit  |
------------------------------+-----------------------------

Comment (by bradyvercher):

 Like @here mentioned, that snippet in the Codex article will break. Or
 here's a basic example:


 {{{
 function ticket18897_pre_get_posts( $q ) {
         if ( is_admin() || ! $q->is_main_query() ) {
                 return;
         }

         // Example settings.
         $offset = 2;
         $posts_per_page = 5;
         $page = $q->is_paged ? $q->get( 'paged' ) : 1;
         $q->set( 'posts_per_page', $posts_per_page );

         // Manually calculate the offset.
         $paged_offset = ( $page - 1 ) * $posts_per_page + $offset;

         // This value is $pgstart with old behavior.
         // This value is added to $pgstrt with the new behavior.
         $q->set( 'offset', $paged_offset );

         // When $page = 2
         // Old: $pgstart = 7
         // New: $pgstart = 12
 }
 add_action( 'pre_get_posts', 'ticket18897_pre_get_posts' );
 }}}

 Another common scenario that's likely to break is when displaying a
 different number of results on the first page than subsequent paged
 requests.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/18897#comment:12>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list