Given a WP_Query, how can I get a list of tags?

I don’t think that is possible. The SQL query performed by WP_Query returns only the post objects (and perhaps some metadata), while the tags resides in a different table. When looping through the returned posts in templates you usually put the_tags(); or something similar in your templates, which in turns runs a new database query for each post.

However, you could run a separate query to load the tags prior to running your primary posts query. As far as I know there is no function WordPress API that will let you load the tags of more than one post at the time, but you could accomplish that by directly calling $wpdb.

Something like this might help get you started:

global $wpdb;
$sql = $wpdb->prepare(
    "SELECT DISTINCT $wbdb->terms.* FROM $wbdb->term_relationships
        JOIN $wbdb->posts
            ON $wbdb->posts.ID = $wbdb->term_relationships.object_id
        JOIN $wbdb->term_taxonomy
            ON $wbdb->term_taxonomy.term_taxonomy_id = $wbdb->term_taxonomy.term_taxonomy_id
        JOIN $wbdb->terms
            ON $wbdb->term_taxonomy.term_id = $wbdb->terms.term_id
        WHERE $wbdb->term_taxonomy.taxonomy = 'post_tag'
            AND $wbdb->posts.ID IN (1,2,3,4,5,6,7,8,9,10)"
);
$tags = $wpdb->query($sql);

As you can see this assumes that you know your post IDs beforehand, which you most likely does not. In that case you would need to replace the lists in the parentheses with a subquery, which in it’s simplest form would be SELECT ID FROM $wpdb->posts, but more likely will need to filter/order/limit posts by category, date, etc. A problem I encountered is that my version of MySQL did not support LIMIT in subqueries. If yours doesn’t either it might be hard to accomplish this in one query. Unless you have a lot of posts you could just loop through your WP_Query first and collect the post IDs. In any case, this will be faster than running separate queries for each post since you will only run one tag-related query instead of running one for each post.

Leave a Comment