I run a few sites which include notices and important information in a banner on their home page. I tend to use a custom post type for this, adding my banners and displaying them where I need to in my theme. (If you want to do something similar, it’s explained in this tutorial.)
But invariably my banners have an expiry date. They might contain information about an upcoming event or a vacancy, for example. Once the event has passed or the vacancy has been filled, I have to go into the site and manually trash the post.
It would be so much easier if, when creating posts like these, I could give them an expiry date after which they’ll no longer be visible on my site.
In this tutorial I’ll show you how to do exactly that. There are three steps:
- Create a meta box in the post editing screen for the expiry date.
- Apply the jQuery UI datepicker to the meta box field to enhance the interface.
- Finally, use the
pre_get_postshook to ensure that posts past their expiry date aren’t displayed.
What You Will Need
To complete this tutorial you will need:
- a development installation of WordPress
- a code editor
You’ll create a plugin with all the code necessary for the expiry date, and activate it on your site. So let’s get started!
Setting Up the Plugin
First you need to create your plugin. In the plugins folder in your
wp-content directory, create an empty file called
Open the file in your code editor and add the following to it:
« Add an Automatic ‘Featured Image’ to Blog Posts Based on Category
Showing an ACF field in admin posts dashboard »