Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' ) { 

            $schaft++;

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

        $i++;

endwhile;
wp_reset_postdata();

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

}
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

EDIT

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() ):
    $first_query->the_post();

    // 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 = '' ) ):
        $second_query->the_post();
        // output post from second query
        the_title();
        // reset post global
        $post = $first_query->post;
    endif;

    // output post from first query
    the_title();

    $previous_date = $post->post_date;

endwhile;
share|improve this answer
    
Haha lol that seems a lot easier than what I just did! :D Going to give it a try now :) –  Jesper Purola Jun 18 '12 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? –  Jesper Purola Jun 18 '12 at 15:20
    
@JesperPurola see edit –  Milo Jun 19 '12 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. –  Jesper Purola Jun 19 '12 at 10:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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