1

I am looking for a way to load a single featured post on the first page of my blog. Below this post, I need to be able to display 12 more posts, and then be able to click through the paginated list 12 posts at a time.

So far, I have blog pages set to display 12 posts, and have setup a "featured" category which which can be used in a custom query to load the most recent post with this term, just on the first page.

I want to exclude just this latest post from the main post loop, but still display older featured posts in the list of 12 below.

Is there a hook or offset to be able to achieve this?

1

If I understand your goal correctly, I would create two queries. The first will retrieve only the featured post. We'll display that post on the first page only and store its ID to use later. Then, a second query will get all of the remaining posts.

// first query to retrieve the featured post
$featured_post_id = 0; // later we'll store the featured post's ID here
if(!is_paged()){ // ensure we only see the feature post on the first page
    $first_args = array(
        'post_type'      => array('post'), // change to custom post type if using one
        'post_status'    => array('publish'), // only get the latest published post
        'posts_per_page' => 1, // we'll only get one post
        'cat'            => 666, // put the ID of the 'featured' category here
    );

    $first_query = new WP_Query( $first_args );
    if($first_query->have_posts()) {
        while($first_query->have_posts()) {
            $first_query->the_post();
            $featured_post_id = get_the_ID();
            // your code for displaying the featured post here
        }
    } else {
        return;
    }
}

// second query to retrieve remaining posts (excluding featured post from above)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$second_args = array(
    'post_type'         => array('post'),
    'post_status'       => array('publish'),
    'posts_per_page'    => get_option( 'posts_per_page' ), // will retrieve number of posts based on value set in Wordpress's admin > Settings > Reading
    'paged'             => $paged, // use pagination
    'post__not_in'      => array($featured_post_id), // exclude featured post from first query
);

$second_query = new WP_Query( $second_args );
if($second_query->have_posts()) {
    while($second_query->have_posts()) {
        $second_query->the_post();
        // your code for displaying remaining posts here
    }
} else {
    return;
}
the_posts_pagination(); // your pagination code here
  • Thank you. I ended up using your answer of storing the featured post ID, and then excluding that post. The only difference was that I did it using add_action('pre_get_posts', my_function); and then altered the query with $query->set('post__not_in', $featured_post_id);. – Alexander Holsgrove Nov 4 '17 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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