Taxonomy Archive: Display only one post per term from separate custom taxonomy

After much pounding on the keyboard, I have figured it out. It may not be the best way to accomplish this, but it works and doesn’t require any additional DB queries.

I used the code above to generate a list of the models I needed to display.

I then used this code to make everything happen.

foreach ($modelList as $model) :

$count = 0;
echo '<h2>' . $model . '</h2>';

// Run the loop again to show the products
while (have_posts()) : the_post();

    // Check that the model name for the post is the same as the model 
    if ( has_term( $model, 'models') ) :

        // Show only first item in loop
        if ($count == 0) :

            get_template_part( 'content', 'model' );                                                            

        endif;

        $count++;

    endif; //has_term                   

endwhile;

endforeach;

Let me explain a little here:

I created a foreach loop for the model list, then inside I ran the standard loop to show all the posts. To make sure posts were grouped by model name, I used has_term() to make sure the model name of the posts matched the item in the array. Then I added a counter to the while loop to make sure only the first post was shown.