WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#34060 closed defect (bug) (fixed)

WP_Query zero offset parameter doesn't override paged

Reported by: mazurstas Owned by: boonebgorges
Milestone: 4.4 Priority: normal
Severity: normal Version: 4.3.1
Component: Query Keywords: has-patch commit
Focuses: Cc:
PR Number:

Description

When offset parameter is set to zero paged parameter is taken into account.

Attachments (2)

patch.diff (467 bytes) - added by mazurstas 4 years ago.
34060.diff (3.7 KB) - added by boonebgorges 4 years ago.

Download all attachments as: .zip

Change History (13)

@mazurstas
4 years ago

#1 @boonebgorges
4 years ago

  • Milestone changed from Awaiting Review to 4.4
  • Owner set to boonebgorges
  • Status changed from new to assigned

Hi mazurstas - Welcome to Trac! Good catch here.

#2 @boonebgorges
4 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 34697:

WP_Query should not ignore an offset of 0.

Props mazurstas.
Fixes #34060.

#3 @westonruter
4 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

I just found that r34697 breaks code that uses get_posts( array( 'paged' => $x ) ) since offset will default to 0 it will always override paged.

This breaks, for instance, the sample code provided at https://vip.wordpress.com/documentation/writing-bin-scripts/#faq

#4 @westonruter
4 years ago

A workaround is to explicitly pass a non-numeric offset, for example:

<?php
$posts = get_posts( array(
    'paged' => $x,
    'offset' => null,
) );

But this is not ideal.

#5 @boonebgorges
4 years ago

@westonruter offset should not default to 0. By default, it shouldn't be set. Do you have a filter or something else that's setting offset to 0?

#6 @westonruter
4 years ago

@boonebgorges I'm using get_posts() without 'suppress_filters' => false, and this function has an offset of 0 among its $defaults:

<?php
function get_posts( $args = null ) {
        $defaults = array(
                'numberposts' => 5, 'offset' => 0,  // <========= HERE
                'category' => 0, 'orderby' => 'date',
                'order' => 'DESC', 'include' => array(),
                'exclude' => array(), 'meta_key' => '',
                'meta_value' =>'', 'post_type' => 'post',
                'suppress_filters' => true
        );
        /* ... */

#7 @boonebgorges
4 years ago

Ah right, get_posts(). (Damn you, get_posts()!) Let me think about this - it might be able to change the default values of get_posts() without breaking backward compatibility.

@boonebgorges
4 years ago

#8 @boonebgorges
4 years ago

  • Keywords has-patch added

@westonruter As I suspected, it looks like we can just pull the offset=0 default out of get_posts(). The tests in [34060.diff] should demonstrate that the behavior matches pre-[34697] behavior. Can I ask you to verify?

#9 @westonruter
4 years ago

  • Keywords commit added

Qapla'!

Change in 34060.diff offset=>null to be omitted and for paged to be respected.

#10 @boonebgorges
4 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 35417:

Don't specify an offset default in get_posts().

The default value should be a null offset. A 0 default overrides any value
of paged passed to get_posts(). See [34697].

Fixes #34060.

This ticket was mentioned in Slack in #core by bobbingwide. View the logs.


4 years ago

Note: See TracTickets for help on using tickets.