[wp-hackers] Get terms from wp_query when using tax_query

Filippo Pisano filippo.pisano at gmail.com
Wed Feb 12 16:22:50 UTC 2014

Hi all,

I was wondering if there is a smart/quick way to obtain all terms attached
to a post when querying posts through WP_Query();

This is the scenario:

$query = array(
    "post_type" => "post",
    "tax_query" => array(
            "taxonomy" => "my_tax",
            "fields"        => "slug",
            "terms"       => "my-term"

$posts = new WP_Query($query);

It would of great hand if it could also return all terms attached to a post

foreach($posts as $post){
    var_dump( $post->terms['my_tax'] ); // could store all terms grouped by

The only way to obtain the same result, as I know, is to loop through each
post and query for terms attached to it:

foreach($posts as $post){
    $post->terms['my_tax'] = wp_get_object_terms($post, 'my_tax'); // very

What do you think? Is there a way to modify posts structure returned by
wp_query without writing custom sql queries?

Filippo Pisano

More information about the wp-hackers mailing list