Web Analytics

How to Automatically Expire Posts, Pages, and Products in WordPress

by Jason Unger, Founder

Photo by Pixabay via Pexels

Every so often, you’ll find yourself needing to take down content from your WordPress site on a specific date or after a specific time has passed. And while that’s easy to do – just go into the post, page, or product and change its status to “Draft” – it also means you need to actually get into your WordPress admin at the right time to make it happen.

For example, suppose your organization is hosting an event, and registration is open until 3 pm on a particular day. If you’re embedding your event registration form into WordPress, then you’d need to remove the form or edit the post right at 3 pm; no earlier, no later.

Or, consider if you have a WooCommerce store and one of your products has a limited availability window. As soon as that window ends, the product can’t be available for purchase anymore – it needs to come down.

Thankfully, there’s a way to automatically expire posts, pages, and products in WordPress, so your content can be made inaccessible at the exact moment that it needs to be removed.

Post Expirator Automatically Unpublishes Your Content

One of our favorite plugins is Post Expirator, which gives you the ability to set a specific date and time that any piece of content on your website will be automatically unpublished.

When you install and activate the plugin, you’ll see a new box when editing your posts and pages that allows you to set a year, month, day, hour, and minute that your content should be expired, as well as the choice of how the content should be unpublished, including:

  • Draft
  • Delete
  • Trash
  • Private
  • Stick
  • Unstick
  • Categories: Replace
  • Categories: Add
  • Categories: Remove

Set the date and time of the expiration, choose how it should expire, and then save. You don’t need to do anything fancy; your server just needs to be running WP-Cron, the system by which WordPress makes things happen at specific times (like publishing your scheduled posts). Most webhosts support WP-Cron, so if you’re not using a cheap web host, you should be OK.

If you want, you can even display the expiration date and time on the front-end of your content, using the shortcode [postexpirator], and you can also get email notifications about when content has expired.

Scheduling Expiration: Custom Fields and Set Number of Days

The Post Expirator plugin has great developer support; so in addition to the easy-to-use WordPress admin box, we can also build functions that expire content based on other factors.

For example, our clients at Silver Branch Brewing have craft beers with a specific availability window, which they set using two custom fields (First Available Date and Last Available Date). Since their beers are sold online, we have to make sure that after the availability window is over, the product is no longer available to be purchased.

We built a function for them that takes the Last Available Date, converts it to a DateTime class, and then schedules it for expiration by making the product draft. (Insert beer joke here.)

Here’s what the code looks like:

function expire_products($post_ID){
	$posttype = get_post_type($post_ID);
	if ($posttype == 'product') {
		if ( get_post_status($post_ID) == "publish"  ) {
			if (get_field('last_available_date',$post_ID) != ''){
				$last = get_field('last_available_date');
				require_once(ABSPATH . 'wp-content/plugins/post-expirator/post-expirator.php');
				$expiration_date = new DateTime($last);
				$expires = date_format($expiration_date, 'Y-m-d H:i:s');
				$ts = get_gmt_from_date($expires,'U');
				$opts['expireType'] = "draft";
				$opts['id'] = $post_ID;
				$id = $post_ID;
				update_post_meta($post_ID,'_expiration-date', $ts);
				update_post_meta($post_ID,'_expiration-date-options', $opts);
				update_post_meta($post_ID,'_expiration-date-status','saved');
				postexpirator_schedule_event($id,$ts,$opts);
			}
		}
	}
}
add_action('save_post', 'expire_products', 10);

Here’s how it works:

  • we’re only looking for “products,” not posts or pages or anything else
  • as we save the post, if the status is published, and if we have some content in our custom field ‘last_available_date’, then
  • we convert that date to a DateTime, reformat it for Post Expirator, and set the Expiration Type (draft in this case)
  • finally, we save our expiration date custom fields (set by the plugin), and schedule the expiration

We did a similar type of function when we built out JConnect for The Jewish Federation of Greater Washington and its user-generated content.

In this case, they wanted to expire events after 90 days of being posted (if the event was marked as a “tentative” event). Here’s what that code looks like:

function expire_tentative_events($post_ID){
	$posttype = get_post_type($post_ID);
	if ($posttype == 'events') {
		if ( get_post_status($post_ID) == "publish"  ) {
			if (get_field('tentative_event',$post_ID) == 'yes'){
				require_once(ABSPATH . 'wp-content/plugins/post-expirator/post-expirator.php');
				$expiration_date = new DateTime('now +90 days');
				$expires = date_format($expiration_date, 'Y-m-d H:i:s');
				$ts = get_gmt_from_date($expires,'U');
				$opts['expireType'] = "draft";
				$opts['id'] = $post_ID;
				$id = $post_ID;
				update_post_meta($post_ID,'_expiration-date', $ts);
				update_post_meta($post_ID,'_expiration-date-options', $opts);
				update_post_meta($post_ID,'_expiration-date-status','saved');
				postexpirator_schedule_event($id,$ts,$opts);
			}
		}
	}
}
add_action('save_post', 'expire_tentative_events', 10);

The biggest difference here is that we’re setting the expiration date to be “now” plus 90 days, so it will work consistently no matter when their user-generated content is added.

Do We Really Need A Plugin for This?

You know our philosophy – less is more. You don’t always need to install a plugin to solve an issue, and just because you can, doesn’t mean you should.

So if you’re considering how this plugin might fit into your website, ask yourself: do I, my team, or my users post enough content that needs to be removed from the site on a regular basis to warrant using this plugin?

If the answer is yes, great; we feel confident in recommending this plugin.

If you don’t need to remove content from your site on a regular basis, or if when you do it’s usually not at a specific date and time, then you probably don’t need this plugin.

Generally, you shouldn’t remove too much content from your website. If Google crawls it, then there will be links to it, and removing your content can hurt your search engine rankings.

But in some cases – like the two documented above – there is a use case for this plugin, and it’s the right option.

About Jason Unger

Jason Unger is the Founder of Digital Ink. He built his first website on Geocities, and hasn't looked back since.

Other Stories You May Like

What’s your story?

Let’s share it with the world.

Let’s Do This

Close Window
All the Cool Kids are Doing it

Sign Up for the Digital Ink Newsletter

All the cool kids are doing it.