Ask your WordPress questions! Pay money and get answers fast! (more info)

'dual' loop to include featured cat in first post WordPress

  • SOLVED

What I am trying to do:

I have a 'dual' loop setup so that I can display the 4 most recent posts from my custom post-type 'photogallery' on the top, and then continue to loop through the rest of the posts.

example:

<?php $my_query = new WP_Query('post_type=photogallery&posts_per_page=4'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate[] = $post->ID;?>

<!-- do stuff -->

<?php endwhile; ?>

<?php if (have_posts()) : $count = 0; ?><? while (have_posts()) : the_post();
if (in_array($post->ID, $do_not_duplicate)) continue; $count++; ?>

<!-- do more stuff -->

<?php endwhile; else: ?>
<?php endif; ?>


What I need:

On the top loop I want to show the 4 most recent posts from the post_type photogallery with the term 'featured' from the custom taxonomy 'gallerycategories'.

I don't want those posts duplicated in the lower loop, and I want my lower loop to continue through the posts just like it does now.

**This is used on my photogallery archive.php page, so I need to show all the photogallery posts in the lower loop, not just one's with the term 'featured', yet still make sure to exclude the featured posts that are on the top loop from the bottom loop.

What I am missing:

I don't know how to add in the part about the term 'featured' into the top portion of the loop. When I try to insert my logic for the term, it ends up also passing to the lower loop, thus only showing posts from the featured term. Everything else works perfectly.

thank you

Answers (1)

2010-12-04

Nilesh shiragave answers:

Hi

Try this


<?php $my_query = new WP_Query('post_type=photogallery&posts_per_page=4'); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>



<!-- do stuff -->



<?php endwhile; ?>



<?php if (have_posts()) : $count = 0; ?><? while (have_posts()) : the_post();

if( $post->ID == $do_not_duplicate ) continue; $count++; ?>



<!-- do more stuff -->



<?php endwhile; else: ?>

<?php endif; ?>


shawn comments:

That is exactly what I have already. I need the logic added to the loop to only show 4 posts from the post_type photogallery with the term 'featured' from the custom taxonomy 'gallerycategories', and then show the rest of the posts below.

I don't see any of that logic in there.


Nilesh shiragave comments:

so posts are displaying without duplicating in the second loop right? Only issue is with the first loop to display post_type photogallery with term 'featured' ?


shawn comments:

yes, the loop is working perfectly without duplicating posts. Just need first loop to only show term featured, and make sure those 4 are not duplicated in 2nd loop


Nilesh shiragave comments:

Try this instead of the first query


<?php $my_query = new WP_Query(array('post_type' => 'bookphotogallerys', 'gallerycategories' => 'featured','posts_per_page'=>4)); ?>


Nilesh shiragave comments:

Try this instead of the first query


<?php $my_query = new WP_Query(array('post_type' => 'photogallery', 'gallerycategories' => 'featured','posts_per_page'=>4)); ?>


Nilesh shiragave comments:

Try this instead of the first query


<?php $my_query = new WP_Query(array('post_type' => 'photogallery', 'gallerycategories' => 'featured','posts_per_page'=>4)); ?>


shawn comments:

Thank you, works perfectly, now I see where I was going wrong earlier, dumb mistake...

Just in case your interested, I have a similar unanswered question:
http://wpquestions.com/question/show/id/1258

Picking you as winner on this one as it works.