Make WordPress Core

Opened 7 years ago

Closed 5 years ago

Last modified 4 years ago

#41246 closed enhancement (fixed)

Filter to short circuit term query 'terms_pre_query'

Reported by: jarocks's profile jarocks Owned by: adamsilverstein's profile adamsilverstein
Milestone: 5.3 Priority: normal
Severity: normal Version: 4.6
Component: Taxonomy Keywords: has-patch has-unit-tests has-dev-note
Focuses: Cc:


It would be nice to be able to effectively short-circuit a terms query so that you could return results from an external data source (WP_Query already has a filter like this in the form of posts_pre_query). My patch would add a similar filter terms_pre_query into the ::parse_query() method of the WP_Term_Query class.

This is more a proof of concept than anything else, I'm not sure if it makes sense to leave the burden of handling term caching as well as the hide_empty and the pad_counts parameters on the callback function. Hopefully some more experienced contributors will have some better insight into that.

Attachments (4)

41246.diff (3.9 KB) - added by jarocks 7 years ago.
41246.2.diff (1.2 KB) - added by spacedmonkey 5 years ago.
41246.3.diff (2.6 KB) - added by spacedmonkey 5 years ago.
41246.4.diff (2.4 KB) - added by adamsilverstein 5 years ago.

Download all attachments as: .zip

Change History (20)

7 years ago

#1 @jarocks
7 years ago

  • Summary changed from Filter to short circuit term query to Filter to short circuit term query 'terms_pre_query'

#2 @boonebgorges
7 years ago

  • Milestone changed from Awaiting Review to Future Release

Hi @jarocks - Thanks for the ticket! I like the idea.

For reference, #36687 was the ticket that introduced posts_pre_query.

I'm unsure off the top of my head how best to handle all the post-query-processing that happens in WP_Term_Query. By way of comparison, WP_Query continues to perform all of its own post_status post-processing on posts provided by external data sources. We want to try to strike a balance between total customizability on the one hand, and giving developers all the conveniences of core queries on the other.

As a way forward, could I suggest that you write a sample implementation? It's fine to use dummy data, of course. But I think it would be very helpful to get a sense of how the filter would work best. Ideally, the dummy data would be hierarchical, so we can get a sense of how count padding etc would work in practice. Since the data doesn't like in WP, it's likely that the callback function will *have* to handle all of this stuff, but it'll be a helpful exercise to see this in practice.

5 years ago

#3 @spacedmonkey
5 years ago

  • Keywords has-patch added
  • Version set to 4.6

Updated the patch. 41246.2.diff to be more similar to #44169.

Looping @adamsilverstein as he owned that.

#5 @adamsilverstein
5 years ago

Thanks @spacedmonkey this looks good to me.

@boonebgorges when you have a chance, can you review 41246.2.diff and the code path in get_terms. As. This looks good to me, however I'm uncertain whether we need to be concerned about priming caches with returned data or updating term counts.

#6 @boonebgorges
5 years ago

Thanks for the ping, @adamsilverstein . 41246.2.diff seems OK to me. Regarding caching, I'd say that core should definitely not be caching the results of short-circuited queries - this kind of "pre" filter hands over all such responsibilities to the plugin.

I'm not sure what you mean by "updating term counts". Do you mean _update_post_term_count() etc? These are not term counts but *object-in-term* counts, and they don't touch the WP_Term_Query results. (They do depend on the term_taxonomy_id from WP's database tables, but again, any plugin using this filter is going to have to figure out how to reconcile this information on its own.) Or by "term counts" do you mean get_terms( fields=count )? In this case too, I'd say that the terms_pre_query callback will just have to be smart enough to detect 'count' queries and return the right results - not much that we can do in core.

5 years ago

#7 @spacedmonkey
5 years ago

  • Keywords has-unit-tests added
  • Milestone changed from Future Release to 5.3
  • Owner set to adamsilverstein
  • Status changed from new to assigned

Update the patch with 41246.3.diff. This is basically a copy and paste from #45749.

I have assigned this to @adamsilverstein to review. But I would also like feedback from @boonebgorges .

#8 @boonebgorges
5 years ago

  • Keywords commit added

Approach in 41246.3.diff looks good to me.

#9 @adamsilverstein
5 years ago

@jarocks and @spacedmonkey nice work here! Thanks for reviewing @boonebgorges its very comforting having your feedback.

I appreciate how concise the code is. The docs for this can also be simpler than other pre- filters since there aren't pagination details or counts to update.

All tests are passing - - I'm going to do some more local testing, then get this committed!

#10 @adamsilverstein
5 years ago

41246.4.diff slight whitespace cleanup. remove references to sites.

#11 @adamsilverstein
5 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 45584:

Taxonomy: add a new 'terms_pre_query' filter to short circuit WP_Term_Query 'get_terms' queries.

Add a new terms_pre_query filter which returns null by default. Return a non-null value to bypass WordPress's default get_terms queries.

Props jarocks, boonebgorges, spacedmonkey.
Fixes #41246.

#12 @adamsilverstein
5 years ago

  • Keywords needs-dev-note added

#13 @desrosj
5 years ago

#47591 was marked as a duplicate.

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

5 years ago

#16 @desrosj
4 years ago

  • Keywords has-dev-note added; commit needs-dev-note removed
Note: See TracTickets for help on using tickets.