Deactivate post after certain date

, ,

Here is a little function that upon loading of site checks to see if the date is past the set date for that post id.  If so then it sets that post to draft mode and removes the post from the live site.

function deactivate_post_check() {
	$post_ids = array('345' => '2017-08-15');
	$cur_date = new DateTime();
	if ($shutoff_date < $cur_date) {
		foreach ($post_ids as $post_id => $date_value) {
			$shutoff_date = new DateTime( $date_value );
			$post_info = get_post( $post_id, 'ARRAY_A');
			if ($post_info['post_status'] == 'publish') {
				$post_info['post_status'] = 'draft';
				wp_update_post( $post_info );
			}
		}
	}
}

add_action('init', 'deactivate_post_check');

 

Skills

Posted on

August 15, 2017

Submit a Comment

Your email address will not be published. Required fields are marked *