WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 8 weeks ago

#35476 accepted enhancement

Allow posts to be filtered before return when `fields` parameter is used with `WP_Query`

Reported by: tlovett1 Owned by: adamsilverstein
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Query Keywords: dev-feedback has-patch has-unit-tests needs-dev-note reporter-feedback needs-refresh
Focuses: Cc:

Description

Right now, when the fields parameter is used with WP_Query ("ids" or "id=>parent"), the returned posts are not filterable at all. Later in WP_Query, the_posts can be used to filter returned posts. This functionality should be mimicked with fields queries.

The use case for this is in plugins that bypass MySQL in WP Query (Elasticsearch). Fields queries right now can not be bypassed.

Patch attached.

Attachments (5)

fields-the-posts.diff (2.0 KB) - added by tlovett1 5 years ago.
Add fields_the_posts filters
fields-the-posts2.diff (2.0 KB) - added by tlovett1 5 years ago.
Use apply_filters_ref_array
35476.diff (2.6 KB) - added by adamsilverstein 5 years ago.
35476.2.diff (1.5 KB) - added by adamsilverstein 5 years ago.
35476.3.diff (3.2 KB) - added by welcher 5 years ago.
Adding some unit tests

Download all attachments as: .zip

Change History (17)

@tlovett1
5 years ago

Add fields_the_posts filters

#1 @adamsilverstein
5 years ago

  • Keywords dev-feedback has-patch needs-unit-tests needs-docs added

This seems super useful, thanks!

When I installed the patch I got this error (twice) when running phpunit:
PHP Fatal error: Call-time pass-by-reference has been removed... (L 3534 & 3534) - looking at the other filters, the ones passing $this by reference all use apply_filters_ref_array

@tlovett1
5 years ago

Use apply_filters_ref_array

#2 @tlovett1
5 years ago

Attached another patch. Thanks Adam!

#3 @adamsilverstein
5 years ago

  • Owner set to adamsilverstein
  • Status changed from new to accepted

#4 @adamsilverstein
5 years ago

  • Keywords needs-docs removed

35476.diff :

  • Add docblocks for the new hook
  • Regenerate from trunk folder

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


5 years ago

#6 @adamsilverstein
5 years ago

35476.2.diff is a more concise patch: if the filter returns anything but false we return that, otherwise behavior is unchanged. Also, the test_query_fields_integers test now passes.

@welcher
5 years ago

Adding some unit tests

#7 @welcher
5 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

#8 @chriscct7
5 years ago

  • Version trunk deleted

#9 @adamsilverstein
3 months ago

  • Milestone set to 5.7

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


8 weeks ago

#11 @hellofromTonya
8 weeks ago

  • Keywords needs-dev-note added

The patch adds a new filter which will require a mention in the misc dev note.

#12 @adamsilverstein
8 weeks ago

  • Keywords reporter-feedback needs-refresh added
  • Milestone changed from 5.7 to Awaiting Review

@welcher or @tlovett1 - is this filter still needed? Since we added posts_pre_query in WP4.6, plugins like Elasticpress can filter even fields queries effectively as far as I can tell.

Can you let me know if this is still needed?

  • If so, it looks like the code needs to get moved to src/wp-includes/class-wp-query.php now (and I guess tests would move as well).
  • If not, let's closed this ticket.
Note: See TracTickets for help on using tickets.