WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 4 months 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: Priority: normal
Severity: normal Version:
Component: Query Keywords: dev-feedback has-patch has-unit-tests
Focuses: Cc:
PR Number:

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 4 years ago.
Add fields_the_posts filters
fields-the-posts2.diff (2.0 KB) - added by tlovett1 4 years ago.
Use apply_filters_ref_array
35476.diff (2.6 KB) - added by adamsilverstein 4 years ago.
35476.2.diff (1.5 KB) - added by adamsilverstein 4 years ago.
35476.3.diff (3.2 KB) - added by welcher 4 years ago.
Adding some unit tests

Download all attachments as: .zip

Change History (13)

@tlovett1
4 years ago

Add fields_the_posts filters

#1 @adamsilverstein
4 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
4 years ago

Use apply_filters_ref_array

#2 @tlovett1
4 years ago

Attached another patch. Thanks Adam!

#3 @adamsilverstein
4 years ago

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

#4 @adamsilverstein
4 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.


4 years ago

#6 @adamsilverstein
4 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
4 years ago

Adding some unit tests

#7 @welcher
4 years ago

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

#8 @chriscct7
4 years ago

  • Version trunk deleted
Note: See TracTickets for help on using tickets.