Make WordPress Core

Opened 8 years ago

Last modified 3 months ago

#35476 accepted enhancement

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

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

Download all attachments as: .zip

Change History (18)

@tlovett1
8 years ago

Add fields_the_posts filters

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

Use apply_filters_ref_array

#2 @tlovett1
8 years ago

Attached another patch. Thanks Adam!

#3 @adamsilverstein
8 years ago

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

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


8 years ago

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

Adding some unit tests

#7 @welcher
8 years ago

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

#8 @chriscct7
8 years ago

  • Version trunk deleted

#9 @adamsilverstein
3 years ago

  • Milestone set to 5.7

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


3 years ago

#11 @hellofromTonya
3 years ago

  • Keywords needs-dev-note added

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

#12 @adamsilverstein
3 years 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.

#13 @JeffPaul
3 months ago

  • Keywords close added

@adamsilverstein I confirmed with @felipeelia thatEP uses the posts_pre_query filter (see: https://github.com/10up/ElasticPress/blob/dc6a2ea675d0fd992ac8a482df718db47d3b9ac3/includes/classes/Indexable/Post/QueryIntegration.php#L64C49-L64C61 in the get_es_posts method we do check for fields and that is working fine). As such, we can probably close this as wontfix?

Note: See TracTickets for help on using tickets.