wp_query for the first sticky, then display the rest of the posts excluding the first sticky

I’ve been reading this by @nacin and getting to grips with wp_query over query_posts that I used to use.

What I want is:

  1. to put this in a template file

  2. to query all posts of this category, in this case ‘3’

  3. to display, if available, the first result on the page the latest sticky post

  4. after the first sticky, if one is set, display the rest of the posts excluding that sticky if it was set

Problems I have seen are:
– if I do posts_per_page = 1 on the sticky loop, I can not do posts_per_page = -1 on the rest of the posts loop. To workaround this I’ve just set the number to 999.

I should say now that the code I have works. However this is for a very high traffic site, and I want to make sure this is the best way of doing it, and I’m not sure I’m using wp_query right to do this since the original query’s are essentially the same as one another just with and without sticky posts.

global $wp_query;
$wp_query = new WP_Query(array(
    'post_type' => 'post',
    'posts_per_page' => 1,
    'category__in' => 3,
    'post__in'  => get_option( 'sticky_posts' )
));
while ($wp_query->have_posts()) : $wp_query->the_post();
    $exclude_featured = $post->ID;
    echo the_title();
    echo '<br />';
endwhile; 

echo '<br />';
echo '<br />';

global $wp_query;
$args = array_merge(
    $wp_query->query_vars,
    array(
        'post__in' => null,
        'posts_per_page' => 999,
        'ignore_sticky_posts' => 1,
        'post__not_in' => array($exclude_featured)
    )
);
query_posts( $args );
while ($wp_query->have_posts()) : $wp_query->the_post(); 
    if ( $exclude_featured == get_the_ID() )
        continue;
        echo the_title();
        echo '<br />';
endwhile; 

Thanks for any help guys.

3 Answers
3

you could use wp_list_pluck();

if ( $exclude_featured )
    $args['post__not_in'] = wp_list_pluck( $exclude_featured->posts, 'ID' );
    $args['posts_per_page'] = 999;
    query_posts( $args );
endif;
while ( have_posts() ) : the_post();
...

Leave a Comment