Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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 :P

share|improve this answer
    
Thank you, kind sir. –  Ollie Jones Feb 28 '12 at 13:04
    
Did it work out for you Ollie? I'm curious how universal this solution might be, as I have seen this same problem all over the internet. Might be a good tutorial for someone to write :P –  cmegown Feb 28 '12 at 15:25

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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