Make WordPress Core

Opened 5 years ago

Closed 4 years ago

#48174 closed enhancement (invalid)

Paginate a sliced number of posts

Reported by: rafiq4580's profile rafiq4580 Owned by: rafiq's profile Rafiq
Milestone: Priority: normal
Severity: normal Version: 5.2.3
Component: Query Keywords: reporter-feedback close
Focuses: Cc:

Description

<?php
$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 (4)

#1 @rafiq4580
5 years ago

  • Keywords dev-feedback added

#2 @subrataemfluence
5 years 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).

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

#3 @SergeyBiryukov
5 years 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.

#4 @hellofromTonya
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from assigned to closed

Hello @rafiq4580,

Welcome to WordPress Core Trac!

Closing this ticket. Why? It was marked for feedback and close 16 months ago. But don't worry. Please reopen to provide more information and continue the discussion.

Note: See TracTickets for help on using tickets.