Can I schedule WordPress to take down a post?
Everybody knows you can schedule WordPress to put a post up on a specific date but is it possible to schedule WordPress to take down a post on a specific day?
Of course it is but you’ll need to know a little PHP and understand how WordPress works.
Setting up your post for a scheduled takedown
First, you’ll need to add a custom field to your WordPress post. There are a couple ways to do this, the easiest is to add a custom field with the name “takedown” and the date you want WordPress to remove it in the Value field. Technically, the name isn’t important (nor is the date format) if you know what you’re doing but let’s try to work with these two standards for now.
- Set the Custom Field Name to takedown
- Set the Value of the Custom Field to 2010-05-01 (year-month-day)
Now save your post and let’s take a look at the next step.
Detecting the current date
PHP allows us to fetch the current date using the date() function and using some formatting options we can return the value of the date based on the format we need which is date(‘Y-m-d’);. If you add the code <?php date(‘Y-m-d’);> after the very last line of your footer file, you’ll see todays date appear at the bottom of your website.
Now let’s make sure to remove it from the last line and work on comparing the date instead.
Comparing the current date
Now that we know now the actual date, we need to know what day we want the website to stop showing our post. For this, we need to fetch the value stored in takedown using PHP:
$takedown = trim(get_post_meta($post->ID, ‘takedown’, true));
$takedown = trim(get_post_meta($post->ID, ‘takedown’, true));if (strlen($takedown)==10) {// your code here}
For some reason, even if the value of the variable is not found WordPress assigned something too it so instead of using isset() or testing for NULL, I’ve found the safest thing to do is check the string length. Since we know the only variable that is valid will be a YYYY-MM-DD format, testing for a ten character string will ensure we’re on the right track. You could also use checkdate() if you’re server supports it.
Finally, I compare the two dates using the code:
if ($date(‘Y-m-d’) >= $takedown) { //yourcodehere;}
Deleting the post
There are a couple ways in WordPress to change the status of a post or delete it outright, in my case I simply want to convert posts from Published (which means they’re live) to Pending (for my review) so that I can manually delete the posts later if I want.
To do this, we will make use of the WordPress function wp_update_post() like so:
$my_post = array();
$my_post['ID'] = $post->ID;
$my_post['post_status'] = ‘pending’;
wp_update_post($my_post);
This code simply tells WordPress to update the current post and set the status to pending which will convert the post from live to hidden.
If you want to completely delete the post on a schedule, you can do so using the wp_delete_post() function instead of my code above.
The final code
Here’s the full code assembled for you, to make it work you should place it in the single.php file just below the final line of code on your website. It is important to note that this code will only activate after the takedown date, when the specific post is viewed so technically, if a page is to be taken down on the 1st of May but nobody visits until the 10th, it will still be live until the 10th.
$takedown = trim(get_post_meta($post->ID, ‘takedown’, true));if (strlen($takedown)==10) {if ($date(‘Y-m-d’) >= $takedown) {
$my_post = array();
$my_post['ID'] = $post->ID;
$my_post['post_status'] = ‘pending’;
wp_update_post($my_post);
}}








