[wp-hackers] How to avoid multiple calls to wp_get_object_terms
Simon Blackbourn
piemanek at gmail.com
Thu Aug 5 21:29:49 UTC 2010
Hi
I've got a custom taxonomy (resource_types) associated with a custom post
type (resource), and in my loop I'm using wp_get_object_terms to list the
taxonomies each post is in:
$types = wp_get_object_terms( $post->ID, 'resource_types' );
However, this causes a complex database query for every post in the loop:
SELECT t.*, tt.*
FROM aaa_terms AS t
INNER JOIN aaa_term_taxonomy AS tt
ON tt.term_id = t.term_id
INNER JOIN aaa_term_relationships AS tr
ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.taxonomy IN ('resource_types')
AND tr.object_id IN (*POSTIDHERE*)
ORDER BY t.name ASC
I can see that WordPress executes a query earlier before the loop that is
getting all this information anyway:
SELECT t.*, tt.*, tr.object_id
FROM aaa_terms AS t
INNER JOIN aaa_term_taxonomy AS tt
ON tt.term_id = t.term_id
INNER JOIN aaa_term_relationships AS tr
ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.taxonomy IN ('resource_tags', 'resource_types')
AND tr.object_id IN (387, 249, 288, 279, 232, 253, 341, 228, 301, 395)
ORDER BY t.name ASC
What I can't figure out is if there is any way to avoid calling
wp_get_object_terms on every post (maybe accessing the data from this
earlier query somehow)?
Thanks
Simon
More information about the wp-hackers
mailing list