Today i will show you how to set sticky posts automatically. You can do it manually of course under Post > Edit by ticking the option under Visibility Option. However, we usually set sticky posts and forget them. Especially, when it comes to a blog with several posts there can be a lot of sticky posts forgotten out there without a purpose.
I have previously written a post on how to auto-manipulate sticky posts here. There you could make sticky posts disappear from the home page, or you could put the sticky post in its chronological order.
Set Sticky Posts – How it works
Now we go a bit further and customize which post is set as sticky. First, we have to get the list containing the sticky posts. This list, which is actually an array, contains the ids of the already set sticky posts. Then we modify it and finally create a new array of post ids. That new array contains only the last post and -if any- the posts that are scheduled to be published in a specific date and time.
Get Sticky Posts Ids
We store in an array the return value of this.
get_option('sticky_posts')
Then we search this array to find any posts that are planned to be published. Finally, we store all these ids in a new array. That will be set as the new list of sticky posts.
update_option( 'sticky_posts' , $new_sticky_posts)
Post Status Transitions
When does this all happen? Or in WP words, when does the hook trigger? I simply use a feature of WP called Post Status Transitions. An article can be found on WordPress Codex, explaining roughly that these actions check the current and future status of a post. Whether the post is new and is about to be published, or a draft that will be published and so on. Briefly, when the status of a post is changing, the hook is triggered and alters sticky posts array.
Had enough of that? Get the code below.
Set Sticky Posts – The Snippet
For the impatient, here’s the code. Try it in a Child Theme and see what happens.
/* * Modify sticky posts * Keep sticky only the latest post */ function gt_unstick_old_posts( $post_id ) { if ( ! wp_is_post_revision( $post_id ) ) $post_id = $post_id->ID; $new_sticky_posts = array(); $stickies_array = get_option('sticky_posts'); // check current status of post // if not published or not private, make it sticky foreach ( $stickies_array as $stickies ) { $post_current_status = get_post_status( $stickies ); if ( $post_current_status != ( 'publish' || 'private' ) || $stickies == $post_id ) array_push( $new_sticky_posts, $stickies ); } // Do a last check for the current post // If it is not set as sticky, make it a sticky if ( !in_array( $post_id, $new_sticky_posts ) ) array_push( $new_sticky_posts, $post_id ); update_option( 'sticky_posts' , $new_sticky_posts); } add_action( 'draft_to_publish', 'gt_unstick_old_posts' ); add_action( 'future_to_publish', 'gt_unstick_old_posts' ); add_action( 'new_to_publish', 'gt_unstick_old_posts' ); add_action( 'pending_to_publish', 'gt_unstick_old_posts' ); add_action( 'publish_to_publish', 'gt_unstick_old_posts' );
That’s it! Happy testing and of course suggestions are welcome!
Leave a Reply