Loop through all tags & output posts in alphabetical list

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

Read More

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 } ?>

Related posts

Leave a Reply

4 comments

  1. (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
    
    ?>
    
  2. 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
    ?>
    
  3.     $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; 
    
        }
    
        }
    
  4. 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.