I have a page that lists posts grouped by category. Here is the current code:
<?php
$cats = get_terms('category');
foreach ($cats as $cat) {
$cat_id= $cat->term_id;
echo "<h2>".$cat->name."</h2>";
$custom_query = new WP_Query(
array(
'cat' => $cat_id,
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
)
); ?>
<ul>
<?php if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
<li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>
</ul>
<?php } ?>
Is there a more efficient way to do this without so many queries? I found this related question: Most efficient way to list all categories and display a post for each of them?. However, I'm not sure how to implement Milo's suggestion or if it would be applicable.