0

Basic loop question:

I have a custom post type with a custom taxonomy and 3 terms.

On the archive page (archive-custom_post_type.php), I would like to display one post from each taxonomy term.

Is this possible within the standard if (have_posts()) : while (have_posts()) : the_post(); loop, or do I have to make separate loops for each one?

  • You will need three loops with a custom query in each one. – Abdussamad Oct 6 '12 at 23:14
  • I found this post, which hints at doing it inside a single default loop: basically using foreach inside the default loop. It seems like it must be possible since on a default archive page I'm already looping through the custom post type. The solution in the link is a little more complex than what I'm trying to do, but maybe I can figure out how to apply the same idea. – Gabriel Luethje Oct 7 '12 at 19:44
1

So I went ahead and did the multiple loop thing, as I had done before. I'm always interested in seemingly cleaner/simpler solutions, but sometimes you just have to keep moving. This is the code I'm using:

<?php   
    $args = array(
        'post_type'         => 'projects',
        'project_category'  => 'websites',
        'orderby'           => 'menu_order',
        'showposts'         => 1
    );

    $posts = get_posts( $args );
    foreach ($posts as $post) :  setup_postdata($post); 

?>

// Get the post stuff here

<?php endforeach; wp_reset_postdata(); ?>

I can repeat this as needed, changing the taxonomy (in this case the taxonomy is project_category) term each time.

Please feel free to comment an tell me if this isn't a good way to do it. I'm far from an expert at the PHP side of WordPress.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.