Skip to content

Taxonomy: Fix WP_Term_Query cache miss for equivalent queries#11391

Open
sanketio wants to merge 3 commits intoWordPress:trunkfrom
sanketio:fix/64038
Open

Taxonomy: Fix WP_Term_Query cache miss for equivalent queries#11391
sanketio wants to merge 3 commits intoWordPress:trunkfrom
sanketio:fix/64038

Conversation

@sanketio
Copy link
Copy Markdown

Summary

Fixes a duplicate database query that occurs on the Posts list table (and wherever wp_dropdown_categories() and wp_terms_checklist() are rendered on the same page) due to two semantically identical get_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:

$key = md5( serialize( $cache_args ) . $sql );

wp_dropdown_categories() passes hierarchical => 1 (int), while wp_terms_checklist() passes get => 'all' which parse_query() immediately normalises to hierarchical => false (bool). Both produce identical SQL, yet serialize() 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):

Arg Reason kept
child_of _get_term_children() runs in PHP; not always reflected in SQL
pad_counts _pad_term_counts() runs in PHP and changes the cached data shape
prune_empty_terms Combined (bool)($hierarchical && $hide_empty) — only the conjunction determines PHP pruning
number / offset (hierarchical only) No LIMIT in SQL for hierarchical queries; PHP slices with array_slice()
fields Preserved from existing logic; determines cache value shape

Changes

  • src/wp-includes/class-wp-term-query.php — Rewrites generate_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 cache test methods covering: shared cache entry for equivalent queries, and separate keys for differing prune_empty_terms, child_of, and pad_counts.

Testing

vendor/bin/phpunit tests/phpunit/tests/term/query.php --filter cache

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.

@github-actions
Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props sanketparmar.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant