WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 2 years ago

#27088 new enhancement

Allow per-query filtering of post status objects

Reported by: ericmann Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.8.1
Component: Query Keywords: needs-patch needs-unit-tests
Focuses: performance Cc:

Description

There comes a time where queries need to pull in posts (or CPTs) that have a non-public post status. Unfortunately, there are no filters on get_post_status() or get_post_status_object() to allow this kind of atomic control within WP_Query::get_posts().

To rectify this, we should add (at a minimum) filters on:

  • get_post_status()
  • get_post_status_object()

Additionally, a filter within WP_Query::get_posts() (on the return of get_post_status_object() with the query passed along for context) will allow per-query filtering of the status object if it needs to be overridden for specific reasons.

Attachments (3)

query-filter.diff (1.7 KB) - added by ericmann 4 years ago.
Add 3 new filters to allow atomic control of post status objects.
27088.diff (1.7 KB) - added by nacin 4 years ago.
show-scheduled-posts.php (763 bytes) - added by danielbachhuber 4 years ago.
Show scheduled posts on view

Download all attachments as: .zip

Change History (10)

@ericmann
4 years ago

Add 3 new filters to allow atomic control of post status objects.

#1 follow-up: @nacin
4 years ago

This ticket focuses directly on a solution. I think it would be best to flush out the problem first. There definitely shouldn't be hooks in those functions due to their nature of needing to return exactly and consistently what the post status definition, or in the other case it's little more than a helper for the post_status post property.

Instead, you should be able to control what status Wp_Query thinks the post is, and/or manipulate or bypass those checks via a particular query variable, filter, or some such.

#2 @SergeyBiryukov
4 years ago

query-filter.diff changes the return values of get_post_status() and get_post_status_object() from a string and an object, respectively, to an array.

#3 in reply to: ↑ 1 @ericmann
4 years ago

Replying to nacin:

Instead, you should be able to control what status Wp_Query thinks the post is, and/or manipulate or bypass those checks via a particular query variable, filter, or some such.

I can accept that. This is why the original patch introduced 3 different filters: 1 each for the post status functions, and 1 inside WP_Query::get_posts() itself.

Based on this feedback, perhaps only the third filter is appropriate.

@nacin
4 years ago

#4 @nacin
4 years ago

Two options that I see:

  1. The posts_results filter is sufficient to fake the status of a post, at least in all situations I can picture. the_posts is also a useful hook here. Use cases would be very helpful here.
  1. Or, something like 27088.diff, which I don't love but is closer to what makes sense to me.

@danielbachhuber
4 years ago

Show scheduled posts on view

#5 @danielbachhuber
4 years ago

I ran into this yesterday, in fact :) Here's how I did it: show-scheduled-posts.php

Adding an additional filter to WP Query is a hack, in my opinion, that only addresses the immediate use case. The proper fix is to implement the first part of Nacin's suggestions in #12706. In doing so, we'd define the attributes of a post status, and the behaviors each attribute should exhibit.

#6 @wonderboymusic
4 years ago

  • Keywords needs-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release

Let's try to flesh this out or close it

#7 @chriscct7
2 years ago

  • Focuses performance added
Note: See TracTickets for help on using tickets.