How to group by taxonomy on Custom Post Type archive

You’re almost there. Since the_title inside your custom loop is one of the Post template tags you need to assign the global $post to use the new data from the custom query.

<?php
    global $post; // Access the global $post object.

    // Setup query to return each custom post within this taxonomy category
    $o_queried_posts = get_posts(array(
        'nopaging' => true,
        'post_type' => $custom_post_type,
        'taxonomy' => $category->taxonomy,
        'term' => $category->slug,
    ));
?>

<div id='archive-content'>

<?php
// Loop through each custom post type
foreach($o_queried_posts as $post) : 
    setup_postdata($post); // setup post data to use the Post template tags. ?>

    <div id="post-<?php the_ID(); ?>">

       <h2 class="entry-title"><?php the_title(); ?></h2>


    </div><!-- #post -->
<?php endforeach; wp_reset_postdata(); ?>

</div> <!-- archive-content -->

Leave a Comment