Group Posts by Category in Monthly Archive

So due to issue mentioned by @Tom J Nowell doing this is actually a bit more complicated because it depends on how you want to sort the posts and if you want pagination. If you sort the posts by date, then even if you could group by category, each page of results would likely still have a few from each category.

There is an easy answer though, if you don’t have too many posts and don’t want pagination, which is to loop through the categories and output all the posts in each category. There are examples here and here, the simplest of which looks like this:

$categories = get_categories( array ('orderby' => 'name', 'order' => 'asc' ) );

foreach ($categories as $category){

   echo "Category is: $category->name <br/>";

   $catPosts = new WP_Query( array ( 'category_name' => $category->slug, 'orderby' => 'title' ) ); 

   if ( $catPosts->have_posts() ){

       while ( $catPost->have_posts() ){
          $catPost->the_post();
          echo "<a href="https://wordpress.stackexchange.com/questions/371176/the_permalink()">the_title()</a>";
       }

   }//end if

} //end foreach

wp_reset_postdata();

You need to edit this to render the category heading and posts how you want them.

The downside of this is that it’ll run more queries – one per category, and then if you have a lot of posts this page will be long!