How to make my front page display only the most recent sticky post, plus usual widgets?

If I’m understanding you correctly, you want to show only the most recent sticky post only on the front page. I had the same issue a month or two ago, and got some fantastic help from the community here at WordPress Answers. The solution is to run two loops in your index.php file. One to pull only the most recent sticky post, and the second to display all other type of posts.

Here’s the link, but I will post my code for this problem as well.

<?php get_header(); ?>
<?php get_sidebar( 'left' ); ?>

<?php if ( is_home() && !is_paged() ) : ?>
<div id="post-wrapper">
    <?php
        // Get IDs of sticky posts
        $sticky = get_option( 'sticky_posts' );
        // first loop to display only my single, 
        // MOST RECENT sticky post
        $most_recent_sticky_post = new WP_Query( array( 
            // Only sticky posts
            'post__in'            => $sticky, 
            // Treat them as sticky posts
            'ignore_sticky_posts' => 1, 
            // Order by date to get the most recently published sticky post
            'orderby'             => date, 
            // Get only the one most recent
            'posts_per_page'      => 1
        ) );
        ?>

    <?php while ( $most_recent_sticky_post->have_posts() ) :  $most_recent_sticky_post->the_post(); ?>
        <!-- your code to display most recent sticky -->
    <?php endwhile; wp_reset_query(); ?>

<?php endif; ?>

<?php
    $all_other_posts = array(
        'post__not_in'  => get_option( 'sticky_posts' )
    );

    global $wp_query;
    $merged_query_args = array_merge( $wp_query->query, $all_other_posts );
    query_posts( $merged_query_args );
?>

<?php if( have_posts() ) : ?>
    <?php while( have_posts() ) : the_post(); ?>
        <!-- your code to display all other posts -->
    <?php endwhile; ?>
<?php endif; ?>
</div> <!-- end #post-wrapper -->

Obviously this code isn’t copy-and-paste for everyone. It worked for me in the code structure I had at the time. Also, forgive the nasty formatting 😛

Leave a Comment