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 bunch of posts that each have multiple tags, and I'm trying to find a way to output all of them on a single page, organized under an alphabetical listing of their respective tags. E.g. if Post1 has the tags A, B and D and Post2 has the tags A, C and D, the output would look like this:

Tag A
Post1
Post2

Tag B
Post 1

Tag C
Post2

Tag D
Post1
Post2

EDIT: I've gotten it working with categories, but I'd still love to have it work with tags instead. (All of the excluded IDs are because I'm technically using categories for other organization.) The functional code is:

<?php $cat_args = array(
    'orderby' => 'title',
    'order' => 'ASC',
    'exclude' => '26,27,32,52,36,31,42,38,41'
    );

$categories = get_categories($cat_args);
    foreach ($categories as $category)
    {
    $catID = $category->term_id;
    $catName = $category->name;
    echo '<strong>'.$catName.'</strong>';
        global $post; // required
        $pArgs = array('category' => $catID,'post_type' => 'shows','orderby' => 'title', 'order' => 'ASC');
        $custom_posts = get_posts($pArgs);
        foreach($custom_posts as $post) : setup_postdata($post);  ?>
            <div class="show">
            <a href="<?php the_permalink(); ?>">
                            <?php the_post_thumbnail("show"); ?>
                <h3 class="center"><?php the_title(); ?></h3>
            </a>
            </div>
        <?php endforeach; ?>
        <?php } ?>
share|improve this question
1  
Welcome to WPSE Linn. In what way did what you attempt to do fail? Could you post some code? –  Stephen Harris Mar 26 '12 at 1:35
    
Thanks! I didn't include any code because it just resulted in a blank page, likely because I'd frankensteined together a couple different solutions - I'll add some more code to the original question. –  Linn Mar 26 '12 at 2:09
    
@Linn, you might want to look at this post, it should get you most of the way there. Here is original source of my post that has a little more information and a couple links that might help you out. Cheers! –  David Mar 26 '12 at 3:29
1  
Ended up sorting it out by creating a custom taxonomy for the tags I wanted, since you can't "get_posts" by tag, but you can by custom taxonomy. Thanks so much for the help, guys! I'll add the final code for the record once I'm allowed to answer my own question (it's too long for a comment). –  Linn Mar 26 '12 at 4:24
1  
@Linn tags are simply taxonomies. So you you should be able to get_posts by tag. The tag taxonomy is 'post_tag'. –  Stephen Harris Mar 26 '12 at 8:20

4 Answers 4

up vote 2 down vote accepted

(Untested) but should works with any taxonomy including the 'tag' taxonomy (post_tag). The following example uses the taxonomy with name 'my-taxonomy'.

<?php
//Get terms for this taxonomy - orders by name ASC by default
$terms = get_terms('my-taxonomy');

//Loop through each term
foreach($terms as $term):

   //Query posts by term. 
   $args = array(
    'orderby' => 'title', //As requested in comments
    'tax_query' => array(
        array(
            'taxonomy' => 'my-taxonomy',
            'field' => 'slug',
            'terms' => array($term->slug)
        )
     ));
    $tag_query = new WP_Query( $args );

    //Does tag have posts?
    if($tag_query->have_posts()):

        //Display tag title
        echo '<h2> Tag :'.esc_html($term->name).'</h2>';

        //Loop through posts and display
        while($tag_query->have_posts()):$tag_query->the_post();
            //Display post info here
        endwhile;

    endif; //End if $tag_query->have_posts
    wp_reset_postdata();
 endforeach;//Endforeach $term

?>
share|improve this answer
    
This works if I replace 'my-taxonomy' with 'dates' (the custom taxonomy I created) but not if I use 'post_tag' - still a little confused why the latter won't work. –  Linn Mar 26 '12 at 16:47
    
Ok, final question: how can I order the posts within the while loop by title? I've tried putting orderby=title in a few of the arrays to no avail - I had it working when I was using categories but I can't get it to work with this code. –  Linn Mar 26 '12 at 18:06
    
The $args is an array, you need to use => (see updated answer) –  Stephen Harris Mar 26 '12 at 18:17
    
I thought I was using that (despite my comment syntax) but apparently not, because it's working now. Thank you thank you thank you! –  Linn Mar 26 '12 at 18:29

Here's the final code I used with a custom taxonomy (edited based on Stephen's answer above so it actually works - I was still just querying categories with my old code):

<?php $terms = get_terms('dates');
    foreach($terms as $term):
        $args = array(
        'orderby' => 'title',
        'tax_query' => array(
            array( 
                'taxonomy' => 'dates',
                'field' => 'slug',
                'terms' => array($term->slug)
            )
        )
        );
        $tag_query = new WP_Query( $args );
        if($tag_query->have_posts()):
            echo '<strong>'.esc_html($term->name).'</strong>';
            while($tag_query->have_posts()):$tag_query->the_post(); ?>
                <div class="show">
                    <a href="<?php the_permalink(); ?>">
                        <?php the_post_thumbnail("show"); ?>
                        <h3 class="center"><?php the_title(); ?></h3>
                    </a>
                </div><!-- .show -->
            <?php endwhile;
        endif; //End if $tag_query->have_posts
        wp_reset_postdata();
    endforeach; //Endforeach $term
?>
share|improve this answer
    $tags = get_tags();

    foreach($tags as $tag) {

    echo '<strong>'.$tag->name.'</strong>';

   $args=array( 
  ‘tag__in’ => array($tag->term_id),
   ‘showposts’=>5, 
   ‘caller_get_posts’=>1 
   ); 
   $my_query = new WP_Query($args); 
   if( $my_query->have_posts() ) { 
     while ($my_query->have_posts()) : $my_query->the_post();
     .........
     endwhile; 

    }

    }
share|improve this answer

First you'll need to get all the tags you've used, then pass them to WP_Query and get all the posts.

<?php
$tags = get_tags();
$tag_str = array();
foreach($tags as $tag) {
    $tag_str[] = $tag->name;
}

if(!empty($tag_str)){
    $tag_str = implode('+', $tag_str);
    $my_query = new WP_Query(array('orderby' => 'title', 'order' => 'ASC', $tag => $tag_str));

    if($my_query->have_posts()):
        while($my_query->have_posts()):$my_query->the_post();
            //loop here
        endwhile;
    endif;
    wp_reset_postdata();
} else {
    //do something if no tags found
}
?>

Not tried this code, but should give you an idea. Put the additional checks wherever needed.

share|improve this answer
    
I don't think this does what @Linn was asking... this gets all posts that belong to one of the tag terms. –  Stephen Harris Mar 26 '12 at 8:34
    
Nope... I'm getting all the tags that exist and then loop over them. I guess thats what your code does, but for a particular tag! –  Rutwick Gangurde Mar 26 '12 at 9:00
    
This: $my_query = new WP_Query(array('orderby' => 'title', 'order' => 'ASC', 'tag' => $tag_str)); simply gets all posts with a term in $tag_str, right? You then loop through those posts. There is only one loop as its not wrapped inside a foreach term statement... –  Stephen Harris Mar 26 '12 at 9:05
    
Check my code, I'm getting all the tags and imploding them over a '+' sign to form a string as 'tag1+tag2+...', then passing it to WP_Query... Look for 'all' tags here (codex.wordpress.org/Function_Reference/WP_Query#Tag_Parameters) –  Rutwick Gangurde Mar 26 '12 at 9:08
    
Ok, missed the +. But still, one list. The OP wants a list for each term. This doesn't do that-- or have I missed something :S? –  Stephen Harris Mar 26 '12 at 9:26

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.