Loop through all tags & output posts in alphabetical list

(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

?>

Leave a Comment