Tag template wordpress: cluster by category title

I am modifying the tag template (tag.php) in WordPress. I would like to display a list of posts, grouped by category name.

For example, this is the code which allows me to group the posts by tag:

$posts = get_posts(array(
 'post_type'     => 'post',
 'category'          => $cat_id,
 'tag'   => $tag->slug,
 'posts_per_page'    => -1,
 'meta_key'      => 'title',
 'orderby'     => 'meta_value',
 'order'       => 'ASC'
  ));

if( $posts ): ?>

<?php  echo '<h3 style="margin-top: 1em;"> <a style="color: black;" id="'. 
$tag->name. '">'. $tag->name. '</a></h3>' ;    ?>

Now, this code could be modified in order to group by category title?
Thanks in advance!

Read more here: Tag template wordpress: cluster by category title

Leave a Reply

Your email address will not be published. Required fields are marked *