Make WordPress Core

Opened 13 months ago

Last modified 13 months ago

#48174 assigned enhancement

Paginate a sliced number of posts

Reported by: rafiq4580 Owned by: Rafiq
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.2.3
Component: Query Keywords: reporter-feedback close
Focuses: Cc:


$args = array(
    'post_type'      => 'post',
    'posts_per_page' => '5'

$query = new WP_Query( $args );

This code is triggering all (i.e.3000) the posts and viewing 5 posts in a page. But, there is no way to paginate between between a number of posts (1 to 300).

Change History (3)

#1 @rafiq4580
13 months ago

  • Keywords dev-feedback added

#2 @subrataemfluence
13 months ago

  • Keywords reporter-feedback added
  • Severity changed from critical to normal

@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).

$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.

#3 @SergeyBiryukov
13 months ago

  • Keywords close added; dev-feedback needs-patch removed

comment:2 looks accurate to me, pagination between any number of posts should already be possible using existing query arguments.

Note: See TracTickets for help on using tickets.