I know I can get the terms for a single post using wp_get_post_terms, but is there a way (without writing a loop) to get all the terms in use by an array of posts?

1 Answer
1

That would be wp_get_object_terms. You’d need an array of post id’s as the input, together with the taxonomy you want to retrieve the terms for (let’s say: books). This should work (untested):

$my_query    = new wp_query ($args);
$my_posts    = $my_query->posts;
$my_post_ids = wp_list_pluck ($my_posts, 'ID');
$my_terms    = wp_get_object_terms ($my_post_ids, 'books');

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *