[wp-trac] [WordPress Trac] #48174: Paginate a sliced number of posts

WordPress Trac noreply at wordpress.org
Mon Sep 30 10:59:23 UTC 2019


#48174: Paginate a sliced number of posts
-------------------------------------------------+-------------------------
 Reporter:  rafiq4580                            |       Owner:  Rafiq
     Type:  enhancement                          |      Status:  assigned
 Priority:  normal                               |   Milestone:  Awaiting
                                                 |  Review
Component:  Query                                |     Version:  5.2.3
 Severity:  normal                               |  Resolution:
 Keywords:  dev-feedback needs-patch reporter-   |     Focuses:
  feedback                                       |
-------------------------------------------------+-------------------------
Changes (by subrataemfluence):

 * keywords:  dev-feedback needs-patch => dev-feedback needs-patch reporter-
     feedback
 * severity:  critical => normal


Comment:

 @rafiq4580 thanks for the ticket.
 Sorry if I have misunderstood your question!

 When we use pagination with `WP_Query`, the underlying query does not pick
 all the records in table, rather it puts the pointer to the correct record
 and picks the correct number of posts from there (`posts_per_page` key in
 the associative array).

 {{{#!php
 <?php
 $currentPage = get_query_var('paged');
 $args = array(
                 'post_type'       => 'post',
                 'post_status'     => 'publish',
                 'posts_per_page'  => 6,
                 'order'           => 'DESC',
                 'orderby'         => 'post_date',
                 'paged'           => $currentPage,
 );

 $query = new WP_Query( $args );
 }}}


 Assuming we are on the 7th page, if we see the underlying query that is
 hitting the database at this point (`echo $query->request;`), it looks
 like this:


 ----
 ''SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND
 ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC
 LIMIT 36, 6''


 ----

 The last bit of the above query, i.e. **LIMIT 36, 6** shows that the
 pointer is now on record number 36 and it has picked 6 records
 (`posts_per_page => 6`) from there - meaning it is only picking up the
 exact number of records that has been assigned against `posts_per_page`
 key.

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


More information about the wp-trac mailing list