Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#55207 new defect (bug)

WP_Query returns published sticky posts when post_status is set to draft

Reported by: nomnom99's profile NomNom99 Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Query Keywords: early needs-refresh
Focuses: Cc:

Description

Originally reported here - https://github.com/wp-cli/entity-command/issues/278

Error:
If a sticky post is published, the following query returns the published sticky post:

<?php
<?php
$query = new WP_Query( array(
    'post_status' => 'draft',
) );
?>
?>

Steps to reproduce:

  1. Create a sticky post and publish it
  2. Create a WP_Query with its 'post_status' set to 'draft'
  3. Run the query
  4. Observe that the published sticky post is returned

Expected behaviour:
Setting post_status to draft should not return published sticky posts.

Attachments (1)

patch-1.diff (446 bytes) - added by NomNom99 2 years ago.

Download all attachments as: .zip

Change History (9)

#1 @NomNom99
2 years ago

EDIT:

If this is the intentional behavior, then it would really be helpful to explicitly mention on the [documentation page](https://wordpress.org/support/article/sticky-posts/) that sticky posts will be returned by WP_Query for all post statuses. The current behavior can be confusing to users who are unfamiliar to this feature.

#2 follow-up: @azouamauriac
2 years ago

Hi @NomNom99 , welcome to WordPress Trac! Thanks for the ticket.

I have two thoughts after reading your report;

First: just use "ignore_sticky_post" option and move forward;
On second thought, I think this is a bug, when you make a request to retrieve drafted posts you should only get drafted posts not a posts with other status no matter they are sticky or not.

After some search, the code was introduced here [8546]/#7457, originally called caller_get_posts and renamed ignore_sticky_posts here [15589]/#14624

Last edited 2 years ago by azouamauriac (previous) (diff)

#3 @NomNom99
2 years ago

Hi @azouamauriac , thank you. I've recommended the OP to use ignore_sticky_post as well for the time being.

#4 @hilayt24
2 years ago

Hi @NomNom99, I also faced the same issue in my recent projects. but after some research, I came to know that the sticky post is returned irrelevant to the status of the post or fired query.

Though you are right regarding adding the note to the documentation.

#5 in reply to: ↑ 2 @peterwilsoncc
2 years ago

  • Keywords early added
  • Milestone changed from Awaiting Review to 6.0
  • Version 5.9 deleted

Replying to azouamauriac:

After some search, the code was introduced here [8546]/#7457, originally called caller_get_posts and renamed ignore_sticky_posts here [15589]/#14624

Thanks for digging in to the history.

The version tag applies to when the bug was introduced, as it looks like this bug has been around prior to WP 5.9 I'll remove the version and update it once I have done some additional checking.

Moving on to the 6.0 milestone for visibility. As this involves the WP_Query class, I've also added the early keyword so any bug fix can be well tested prior to release.

#6 @NomNom99
2 years ago

Thanks @peterwilsoncc

I created a patch. It doesn't have any tests for now, figured it'll be better to move ahead once the issue is confirmed.

@NomNom99
2 years ago

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


2 years ago

#8 @peterwilsoncc
2 years ago

  • Keywords needs-refresh added
  • Milestone changed from 6.0 to Future Release

This was discussed during a bug scrub today.

It was decided to take it off the 6.0 milestone to allow time to make a more considered decision around the two points that have been debated above:

a) only post statuses of the type requested should be returned, or
b) sticky posts are returned regardless of the query made, ignore_sticky_posts prevents that.

As the ticket touches WP_Query, it's not possible to rush a decision on this.

Note: See TracTickets for help on using tickets.