How can I use wp_get_object_terms() to get a list of all the terms attached to all posts in the current query?

For example, for the current query I want to get an array of the terms from the "Alfa" taxonomy that are contained in all the queried posts.

wp_get_object_terms($wp_query, 'alfa');

But that only seems to be returning one item in the array...

I am doing this to build an array to cross check one taxonomy with another for a navigation menu, and am currently doing this with the following code but I think there must be a better way.

Please help! Thanks!

$queried_terms = array();
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
    $postid = $post->ID; 

    if( has_term( '', 'alfa', $postid) ) {
        $terms = get_the_terms( $postid, 'alfa' );
        foreach($terms as $term) {
            $queried_terms[] = $term->slug;

endwhile; endif;

$queried_terms = array_unique($queried_terms);
  • Are you passing the whole $wp_query object to the function? I guess that is not correct. Commented Apr 26, 2012 at 5:10
  • I guess I'm just looking to see if the code I posted is the fastest way to get all posts from the query Commented Apr 26, 2012 at 16:15
  • Anything faster would take bunch of custom SQL... Are you interested in that or your current way will do?
    – Rarst
    Commented Apr 26, 2012 at 16:57
  • I have a need for speed! If you know how to make that happen with a custom SQL, I would be forever grateful. Commented Apr 26, 2012 at 21:57
  • I think this and some modifications could make the trick wordpress.stackexchange.com/a/50176/7314
    – Alexey
    Commented Apr 30, 2012 at 8:53

2 Answers 2


I think you're on the right track because wp_get_object_terms() can take an array of IDs for its first argument, it's just that $wp_query is not the array you want.

I can't guarantee that this is more efficient (not my area of expertise), but I believe this [partially-tested] snippet would do what you want with at least one fewer loop and no array_unique():

// get $wp_query
global $wp_query;
// get array of post objects    
$my_posts = $wp_query -> posts;
// make array for the post IDs
$my_post_ids = array();
// loop through posts array for IDs
foreach( $my_posts as $my_post ) {
    $my_post_ids[] = $my_post->ID;
// get the terms
$my_terms = wp_get_object_terms( $my_post_ids, 'alfa' );

wp_get_object_terms() takes a 3rd $args parameter which you may need to set to get the ouput you want, but I'll leave that to you.

UPDATE: This can be even shorter using the new-to-me function wp_list_pluck(). Again this is untested but looks right:

// get $wp_query
global $wp_query;
// get array of post objects    
$my_posts = $wp_query -> posts;
// NEW: make array of the post IDs in one step
$my_post_ids = wp_list_pluck( $my_posts, 'ID' );
// get the terms
$my_terms = wp_get_object_terms( $my_post_ids, 'alfa' );

You can see in the source that this runs the same foreach loops code, but it looks a little nicer.

  • @j-man86 if this solved your problem, make sure to accept it as the answer. Otherwise, update your question and explain why this didn't work.
    – mrwweb
    Commented May 8, 2012 at 19:23

This generalisation of above worked for me:

$args = array( 'cat' = -1 ); // e.g. to get list of posts in any category
$postobjs = get_posts( $args );
$postids = wp_list_pluck( $postobjs, 'ID' );
$taxonomy = 'mytax' // your taxonomy name
$termobjs = wp_get_object_terms( $postids, $taxonomy );
$termlist = array_unique( wp_list_pluck( $termobjs, 'name' ) ); // distinct term names

It outputs a unique list of terms in the 'mytax' custom taxonomy. Thanks @mrwweb :-)

  • any idea why this wouldn't wouldn't work for woocommerce and pa_color?
    – Samyer
    Commented Apr 25, 2017 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.