Add an Expiry Date to WordPress Posts

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:

  1. Create a meta box in the post editing screen for the expiry date.
  2. Apply the jQuery UI datepicker to the meta box field to enhance the interface.
  3. Finally, use the pre_get_posts hook 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 tutsplus-post-expiry-date-php.

Open the file in your code editor and add the following to it:

https://code.tutsplus.com/tutorials/add-an-expiry-date-to-wordpress-posts--cms-22665?_ga=2.55475305.375895460.1662704905-1858649747.1647844437
«
»
ACF (3)
Admin Column (4)
Breadcrumbs (2)
CSS (6)
Custom post types (4)
Custom Taxonomies (3)
Customizer (4)
Hooks & Filters (3)
Jquery (9)
LAMP (10)
Login (5)
Meta boxes (6)
Multisite (2)
My Code (1)
Open Graph Meta Tags (1)
Redirects (2)
SEO (4)
Theme Building (5)
Title Tags (2)
Typography (3)
Uncategorised (54)
User Roles & Capabilities (2)
White labeling (9)
Working with Images (11)
Working with videos (2)
WP Database (1)