WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 21 months ago

#29942 closed defect (bug) (duplicate)

wp_get_object_terms - no caching mechanism in place - results in duplicate queries

Reported by: webgeekconsulting Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.0
Component: Taxonomy Keywords: needs-unit-tests has-patch
Focuses: performance Cc:

Description

wp_get_object_terms() isn't caching queries which results in duplicate queries against the database. See the following debug info:

SELECT t.term_id FROM hf9v_terms AS t INNER JOIN hf9v_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN hf9v_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('product_category') AND tr.object_id IN (229) ORDER BY t.name ASC
include('wp-admin/edit-form-advanced.php'), do_meta_boxes, call_user_func, post_categories_meta_box, wp_popular_terms_checklist, wp_get_object_terms 
SELECT t.term_id FROM hf9v_terms AS t INNER JOIN hf9v_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN hf9v_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('product_category') AND tr.object_id IN (229) ORDER BY t.name ASC
include('wp-admin/edit-form-advanced.php'), do_meta_boxes, call_user_func, post_categories_meta_box, wp_terms_checklist, wp_get_object_terms

I've attached a patch that creates a cache of the database query - similar to what is being suggested in ticket #29894.

Attachments (1)

taxonomy.php.patch (3.9 KB) - added by webgeekconsulting 4 years ago.
taxonomy.php patch

Download all attachments as: .zip

Change History (6)

@webgeekconsulting
4 years ago

taxonomy.php patch

#1 @boonebgorges
4 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release

Thanks for the patch! As in #29894, I like the consolidation of cache keys. And as in #29894, I have a couple questions and concerns:

  • It looks at a glance like the second query (2332 right hand side in https://core.trac.wordpress.org/attachment/ticket/29942/taxonomy.php.patch) is pulling up information that is available in the main query being fired earlier. No need for a second query, and no need for a separate cache. This needs testing.
  • Unit test coverage for wp_get_object_terms() is pretty lackluster. I'm very wary of making these sorts of changes until we at least get basic coverage.

#2 @chriscct7
3 years ago

  • Keywords has-patch added

#3 @dd32
2 years ago

Just run into this again, if there's anyone out there who wants to put a little time into digging into Taxonomies, this could be an interesting patch to continue with @boonebgorges concerns :)

#4 @dd32
2 years ago

Should also mention for others; You can use get_the_terms() which is the cached variant of wp_get_object_terms().

#5 @boonebgorges
21 months ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from new to closed

This ended up being resolved as part of the WP_Term_Query migration in #37198.

Note: See TracTickets for help on using tickets.