Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a new WP_Query that I use to generate a custom loop and display a set of posts. One of the things the query does is provide pagination.

Before I display the queried posts though, I'd like to get a list of all the tags for the posts found. I know I can do this by looping through each of the posts and then removing duplicates, but I was hoping for a more elegant approach.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
thanks for the comprehensive answer! –  Jack Jan 29 '13 at 14:21
add comment

For the sake of posterity, I ended up just iterating through the loop twice: once to get the tags, and then to actually display the page.

Here's my code:

if ( have_posts() ) :
 // get tags for found posts
 $recent_tags = array();
 while ( $loop->have_posts() ) : $loop->the_post();
  foreach(get_the_tags() as $t) $recent_tags[$t->slug] = $t->name; // this adds to the array in the form ['slug']=>'name'
 endwhile; 

 // de-dupe
 $recent_tags = array_unique($recent_tags);
 // sort
 natcasesort($recent_tags);

 // do something with the array

 while ( $loop->have_posts() ) : $loop->the_post();

 // normal wp loop
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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