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

All we need is an easy explanation of the problem, so here it is.

WP 3.3.1, Suffusion 4.0.2

I am trying to figure out how to make my front page display only the most recent post marked sticky. I can’t find how to do this in the documentation.

What I’m trying to do is use a combination of future post publication dates and expirations (via the Atropos plugin) to control what shows up on my front page.

Any suggestions? Is there a plugin that has a shortcode that lets me insert the most recent stick post?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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 😛

Method 2

Thanks so much for this! It was very helpful. One comment: if you want to have the most recent post display as a featured post (i.e., if a post hasn’t been made sticky), and you don’t want to have this post duplicated in the regular list, then in the first loop change:

while ( $most_recent_sticky_post->have_posts() ) :  $most_recent_sticky_post->the_post(); 

to:

while ( $most_recent_sticky_post->have_posts() ) :  $most_recent_sticky_post->the_post(); 
$do_not_duplicate = $post->ID; 

and in the second loop, change:

if( have_posts() ) : while( have_posts() ) : the_post();

to

if( have_posts() ) : while( have_posts() ) : the_post();
if( $post->ID == $do_not_duplicate ) continue;

Bruce

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply