Taxonomy: Fix WP_Term_Query cache miss for equivalent queries#11391
Taxonomy: Fix WP_Term_Query cache miss for equivalent queries#11391sanketio wants to merge 3 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Summary
Fixes a duplicate database query that occurs on the Posts list table (and wherever
wp_dropdown_categories()andwp_terms_checklist()are rendered on the same page) due to two semantically identicalget_terms()calls producing different cache keys.Problem
WP_Term_Query::generate_cache_key()hashes the full set of sanitised query args together with the SQL:wp_dropdown_categories()passeshierarchical => 1(int), whilewp_terms_checklist()passesget => 'all'whichparse_query()immediately normalises tohierarchical => false(bool). Both produce identical SQL, yetserialize()produces two different strings → two different cache keys → two identical DB queries on every page load.Solution
Replace the wide arg-serialisation approach with SQL + only the args that drive PHP-level post-processing (which is not captured by the SQL):
child_of_get_term_children()runs in PHP; not always reflected in SQLpad_counts_pad_term_counts()runs in PHP and changes the cached data shapeprune_empty_terms(bool)($hierarchical && $hide_empty)— only the conjunction determines PHP pruningnumber/offset(hierarchical only)LIMITin SQL for hierarchical queries; PHP slices witharray_slice()fieldsChanges
src/wp-includes/class-wp-term-query.php— Rewritesgenerate_cache_key()to key on SQL + a minimal set of PHP-post-processing args.tests/phpunit/tests/term/query.php— Adds four new@ticket 64038/@group cachetest methods covering: shared cache entry for equivalent queries, and separate keys for differingprune_empty_terms,child_of, andpad_counts.Testing
Trac ticket: https://core.trac.wordpress.org/ticket/64038
Use of AI Tools
AI assistance: Yes
Tool(s): GitHub Copilot (Claude Sonnet 4.6)
Used for: The problem analysis, approach selection, implementation, and test cases were reviewed and verified by the human author before submission. All code changes have been manually checked for correctness against the WordPress codebase.
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.