One Sticky Post in Wordpress

(2012)

As of Wordpress 2.7, there's a nice little feature called "Sticky Posts" that lets you specify that a particular post should be pulled out of its regular position in reverse-chronological order and placed at the top. It's a great way to draw attention to a particular post or give it special styling.

One tiny downside is that when you add a new sticky post, you also need to go remove the old ones. That's perfectly fine if you want several sticky posts on your site, but for my purposes, I only wanted one sticky post at a time.

So here's a quick function that automatically unsets old sticky posts so you never have more than one at a time. You can add this directly to your functions.php file or wrap it in a plugin. As always, use at your own risk.

add_action( 'admin_init',  'my_sticky_admin_init' );
function my_sticky_admin_init() {
    add_action( 'save_post', 'my_sticky_save_post' );
}
function my_sticky_save_post( $post_id ) {
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }
    $sticky = isset($_POST['sticky']) && $_POST['sticky'] == 'sticky';
    if( $sticky ) {
        $sticky_posts = array($post_id);
        update_option('sticky_posts', $sticky_posts);
    }
}