5

My ultimate goal is to list the latest 5 posts, regardless of category.

I would first like to make a WP_Query to get the latest two posts and insert them in to a specific HTML structure.

Then I would like to make another WP_Query to get the next three posts and insert them in to a differing HTML structure.

I've tried a few code snippets and what happens is each WP_Query starts from the latest post in both HTML structures. Is there an arg that I can use to specifically tell the second query to skip the first two posts?

2

2 Answers 2

7

Run one wp_query for post_per_page = 2 and get the IDs of these 2 posts in an array to be excluded in the next 3 posts needed

<?php

    // The Query
    $next_args = array(
                    'post_type' => '<your_post_type>',
                    'post_status' => 'publish',
                    'posts_per_page'=>2,
                    'order'=>'DESC',
                    'orderby'=>'ID',
                    );

    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) {
        $not_in_next_three = array();
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            //your html here for latest 2
            $not_in_next_three[] = get_the_ID();
        }

    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

Now the array created above to be excluded in the wp_query fetching the next 3 posts

// The Query
$next_args = array(
                'post_type' => '<your_post_type>',
                'post_status' => 'publish',
                'posts_per_page'=>3,
                'order'=>'DESC',
                'orderby'=>'ID',
                'post__not_in'=>$not_in_next_three
                );
$next_the_query = new WP_Query( $next_args );

// The Loop
if ( $next_the_query->have_posts() ) {
    while ( $next_the_query->have_posts() ) {
        $next_the_query->the_post();
        //your html here fir latest next 3

    }

} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

?>
5
  • 2
    Add some explanation to yor code and method you are using. This can clearly be done in one query, not 2 Feb 3, 2016 at 10:18
  • 1
    Yeah can be done in one query .. but the question owner has asked for 2 wp_query .. that is why... Feb 3, 2016 at 10:19
  • check the edited answer now @peiter Goosen Feb 3, 2016 at 10:23
  • I solved this problem by performing two WP_Query on the same page and using an offset parameter in the second set of args. I'd like to know how to use one query and achieve the same result.
    – C. Ruben
    Feb 10, 2016 at 22:59
  • It can be done in one wp_query by increasing counter and changing the HTML structure on basis of counter . Feb 11, 2016 at 12:56
3

You can use WP_Query's pagination paramters.

However, using two queries to accomplish this objective is inefficient. A better solution it to use one query and alter your markup based on the value of WP_Query's $current_post property, which (when used in the loop) reflects the index of the post currently being processed within the current page of results.

Your Answer

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

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