public

Wordpress Post Expirator

  • Download Gist
wordpress_post_expirator.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<?php
 
/*
This function assumes a custom field named 'expiration' with a human friendly date/time.
*/
 
function is_post_expired($post_ID = null){
if(!$post_ID) global $post;
$post_ID = $post_ID ? $post_ID : $post->ID;
//Human Friendly Expiration Date
$expiration = get_post_meta($post_ID, 'expiration', true);
//Adjust server time for your timezone
date_default_timezone_set('American/New_York');
$expiration_timestamp = strtotime($expiration);
$time_left = $expiration_timestamp - time();
 
if($time_left < 0):
if(expire_post($post_ID))
return true;
endif;
}
 
function expire_post($post_ID){
$args = array(
'ID' => $post_ID,
'post_status' => 'draft'
);
if(wp_update_post($args))
return true;
}
?>

Hello everyone,

first thanks so much for sharing this code. I am using the theme Classipress with the child theme Eldorado and just want to know where to insert this php file; into the parent theme or into the child theme. Another question: how to set the custom field "expiration" ?

Thanks in advance

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.