An expiry date is a way to control how long your WordPress posts are visible on the website. After the expiry date, the post can be either deleted or made into a draft. This is how the expiry date works on WordPress. This can be achieved in different ways with or without plugins. In this article, we are going to take a deeper look into the possible methods to accomplish that. If you have been searching for a way to control the article’s expiry time, this is the guide you are looking for.
Why should you set an Expiry date to your WordPress posts?
The post’s expiry date is that much important to you if you are running a website which is mostly concentrated on some deals, coupons or offers. Once the offer or deal is expired, there is no meaning of keeping the post. But you can’t just delete some valuable words written by you by utilizing the most valuable time.
Now, the best option is to set an expiration date on the post which makes the visitors aware of the deal expiration. This is a quick and simple guide that teaches you How to show the expiry date on posts. Follow the simple steps to really get what you are searching for.
Ways to show expiration date to WordPress posts
There are several ways you can implement the expiration date functionality to your WordPress posts. You can choose the most suitable one for your requirements.
Way 1. How to Show the Expiry Date on Posts in WordPress Using Post Expirator Plugin
Post Expirator is a very useful plugin which helps to set a specific expiry date on your posts. The easy to use yet comprehensive plugin has its own ideal methods to set the expiry date and time on each post that you want to set the expiry dates. Follow the steps below to do that.
- Download the Post Expirator plugin from the WordPress plugin directory.
- Install and activate the plugin.
- Now, Go to the specific post for which you want to set the expiry date.
- On the right panel. You might see a Post Expirator section like below screenshot
- Now, Check the ‘Enable Post Expiration checkbox.
- Set the specific date and time for your post.
Now, you have set the expiry date for the post, and it will be something like the above picture. All done, and you can repeat the steps for each post that you want to set the expiration date. To customize the options or the format of the date and time, just go to the Settings > Post Expirator. You will be given the options to customize the plugin.
Way 2. Using custom code
You can also set an expiration date to a post using the wp_update_post() function. Use the following function in your functions.php file.
Make sure to use the correct code block based on whether you want to add an expiration date to a single post only or multiple posts at once.
For single post
function set_post_expiration_date($post_id, $expiration_date) {
$expiration_timestamp = strtotime($expiration_date);
$expiration_date = date('Y-m-d H:i:s', $expiration_timestamp);
$post_data = array(
'ID' => $post_id,
'post_status' => 'future',
'post_date' => $expiration_date,
);
wp_update_post( $post_data );
// Save the expiration date as post meta data
update_post_meta($post_id, 'expiration_date', $expiration_date);
}
// Set the expiration date to 1 month from today at noon
// You can replace the +1 month and noon with days, weeks, years
// and midnight, 8:35pm, 7:00am, etc. respectively
$expiration_date = date('Y-m-d H:i:s', strtotime('+1 month', strtotime('noon')));
// Set the expiration date for post ID 123
set_post_expiration_date(123, $expiration_date);
For multiple posts
function set_posts_expiration_date($posts_and_dates) {
foreach ($posts_and_dates as $post_and_date) {
$post_id = $post_and_date['post_id'];
$expiration_date = $post_and_date['expiration_date'];
$expiration_timestamp = strtotime($expiration_date);
$expiration_date = date('Y-m-d H:i:s', $expiration_timestamp);
$post_data = array(
'ID' => $post_id,
'post_status' => 'future',
'post_date' => $expiration_date,
);
wp_update_post($post_data);
// Save the expiration date as post meta data
update_post_meta($post_id, 'expiration_date', $expiration_date);
}
}
// An array of posts and their expiration dates
$posts_and_dates = array(
array(
'post_id' => 123,
'expiration_date' => '2023-06-14 12:00:00',
),
array(
'post_id' => 456,
'expiration_date' => '2023-06-21 12:00:00',
),
);
// Set the expiration dates to the posts
set_posts_expiration_date($posts_and_dates);
This will set the ‘expiration_date’ post meta. In order to display it in the frontend, use the following code –
$expiration_date = get_post_meta(123, 'expiration_date', true);
if ($expiration_date) {
echo 'This post expires on ' . date('F j, Y', strtotime($expiration_date));
} else {
echo 'This post does not have an expiration date.';
}