[wp-hackers] How to avoid multiple calls to wp_get_object_terms

Mike Schinkel mikeschinkel at newclarity.net
Fri Aug 6 03:33:37 UTC 2010


Can you past the code somewhere (gist, pastebin, etc.) so we can see what you are doing?

-Mike

On Aug 5, 2010, at 5:29 PM, Simon Blackbourn wrote:

> 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
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list