WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 6 years ago

#27142 closed defect (bug) (invalid)

Issues with pagination when offset is set

Reported by: alkinfotech Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8.1
Component: Query Keywords:
Focuses: Cc:

Description

When the offset is set to display latest posts, the first page displays the posts perfectly. But the issue is for the second page onwards. It displays the first page contents only.

Change History (4)

#1 @danielbachhuber
7 years ago

  • Keywords reporter-feedback added

Hi alkinfotech,

Are you using Twenty Thirteen or Twenty Fourteen? If not, can you switch to one of them, and disable all of your plugins, to confirm the bug is in WordPress core and not your theme?

If you do think the bug is in WordPress core, then it would be great to have more detail about how to reproduce. I'd recommend writing the reproduction steps as though you're setting up WordPress for the first time.

#2 @alkinfotech
7 years ago

The issue is identified with the code at wp-includes/query.php.

The paging code is as follows:

if ( empty($qnopaging?) && !$this->is_singular ) {

$page = absint($qpaged?);
if ( !$page )

$page = 1;

if ( empty($qoffset?) ) {

$pgstrt = ($page - 1) * $qposts_per_page? . ', ';

} else { we're ignoring $page and using 'offset'

$qoffset? = absint($qoffset?);
$pgstrt = $qoffset? . ', ';

}
$limits = 'LIMIT ' . $pgstrt . $qposts_per_page?;

}

Note, when offset is there, $pgstrt = $qoffset? . ', ';

That means eg. offset = 4, the pgstrt will always be 4 and hence the limit will be LIMIT 4,[posts_per_page], irrespective of the page selected.

The solution is to modify this line as :

$pgstrt = $qoffset? + ($page - 1) * $qposts_per_page? . ', ';

So for page = 2, offset = 4 & posts_per_page = 10, pgstrt = 14 & limit will be LIMIT 14,10 which will be ideal for page 2 with offset 4.

I am currently using this and it is working perfectly fine.

Wanted to raise the bug so that in the next release, it is covered up.

#3 @bcworkz
7 years ago

This is expected. Pagination uses the offset parameter of LIMIT to fetch the correct posts for any particular page. By explicitly specifying an offset, you take away this possibility from the pagination algorithm. From http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters (emboldened text my emphasis):

Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination

#4 @DrewAPicture
6 years ago

  • Component changed from General to Query
  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

See comment:3

Note: See TracTickets for help on using tickets.