The question probably needs some clarification so here I go: I'm building a carousel displaying the titles of the 5 latest articles.

However, from a specific category (called schaft in my case) it should only display one post even though the 5 latest posts might have this category in there multiple times. If this is the case, it should display the latest one, but skip the following ones an jump to the next item in the query.

I've got this working by using two loop functions. The first one is checking if there are excess schaft categories in the query, skipping to the next one if necessary and repeating the whole function adding 1 to the number of posts the query has to display until it displays 5 posts even without the excess schaft categories. It will then run a function with the loop actually displaying the post titles The problem is that it has to repeat the query a lot of times and I'm pretty sure this is not efficient.

Maybe someone can tell me how to do this the right and efficient way? I've added my code for clarity.

function carouselimages( $postsinloop ) {

if ( !$postsinloop) { $postsinloop = 5; }
$i = 0;
$schaft = 0;

$schaft_duplicate_query = new WP_Query('posts_per_page='.$postsinloop);
while($schaft_duplicate_query->have_posts()) : $schaft_duplicate_query->the_post();

        $category = get_the_category();
        $category = $category[0]->slug;

        if ( $category == 'schaft' ) { 


            if ( $schaft >= 2 ) { continue; }



if ( $i < 5 ) { carouselimages( $postsinloop ); }
if ( $i == 5 ) { carouselloop($postsinloop); }


1 Answer 1


You could do one query for the 4 latest posts excluding the schaft category, then do another query for the latest post in schaft. get the date of the single schaft post and check it against the dates of the other posts as you output them, inserting it in the correct position.


Two loop example:

$category = 1; // category to exclude from 1st / use for 2nd query
$first_args = array(
    'posts_per_page' => 4,
    'category__not_in' => $category
$second_args = array(
    'posts_per_page' => 1,
    'cat' => $category
$first_query = new WP_Query( $first_args );
$second_query = new WP_Query( $second_args );

$previous_date = '';

while( $first_query->have_posts() ):

    // insert post from 2nd query if its post date is greater than this post date
    // but less than the previous, or the previous is empty because it's the first
    if( $second_query->post->post_date > $post->post_date
        && ( $second_query->post->post_date < $previous_date || $previous_date = '' ) ):
        // output post from second query
        // reset post global
        $post = $first_query->post;

    // output post from first query

    $previous_date = $post->post_date;

  • Haha lol that seems a lot easier than what I just did! :D Going to give it a try now :)
    – user17235
    Jun 18, 2012 at 13:58
  • Okay I've been trying to merge those two queries but no luck so far. Do you have any pointers on how I can achieve this?
    – user17235
    Jun 18, 2012 at 15:20
  • @JesperPurola see edit
    – Milo
    Jun 19, 2012 at 3:56
  • Wow Milo! Thanks a lot! It works like a charm! I had no idea I could pull the post data from the query like that. I had the postdata of both posts stored in an array which I merged and then tried sorting them with usort.
    – user17235
    Jun 19, 2012 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.