It’s been over seven years since I last built or maintained a WordPress plugin, thanks in large part to the great community of WordPress plugin developers who share their hard work for the community as a whole. Yet, I finally ran into a situation that wasn’t handled by an available plugin. I patched this in my custom functions.php file with some help from code that I found here (thanks Craige!) and have cruised along for a good while, but I wanted to strengthen my solution while giving back to the WordPress community.
(TLDR – the WordPress community is awesome, and I published this plugin: There Can Be Only One)
My Goal
There should always be one (and only one!) sticky post visible at the top of my home page.
The Setup
I post many links and images on my site, which means the majority of my front page is made up of links elsewhere, and anything that I write is quickly lost in the stream. Sticky posts help to address this, allowing me to designate that a post should be shown at the top of my home page, regardless of whether it is the newest or not. Awesome!
The Challenge
WordPress allows you to have as many sticky posts at any time as you like, which is useful for a lot of people, but requires that I manually remove the sticky designation from older posts. If I didn’t, my home page would fill up with older posts and the links that I like to share would be lost several pages deep into my site.
I want to show a single post that I’ve written at the top of the page and let all other posts, whether written by me, or aggregated from my other sources, to flow in the order in which they were created. I want this behavior to be the same whether I immediately publish a post or a scheduled post is published. There can be only one.
The Solution
As noted above, I had most of what I needed, but it didn’t handle scheduled posts in the manner that I’d like. That code removed the sticky designation from all other posts at the time the post was published or scheduled, so if I set a post to publish a few days into the future, the previous sticky post would immediately lost its sticky designation and fall back into the stream. This isn’t how I want it to work – I want there to be a sticky post at the top of my home page at all times.
So, with the help of Devin Price and a few resources published by the community at large I modified the code to handle the different use cases, then wrapped it up as a plugin. You can now find There Can Be Only One in the WordPress Plugin Directory. This is very specific functionality for a small niche of users, but I hope others find it useful. At the end of the day, this is one of the greatest benefits of a community like WordPress: we can give back and enrich the platform for all, friends and strangers alike.