WP_query to get the first two latest posts, then another loop to get the next three

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();

?>

Leave a Comment

tech