Support

Account

Home Forums General Issues Expire posts to drafts from datepicker date after 1 day?

Solved

Expire posts to drafts from datepicker date after 1 day?

  • Hey everybody, I’m using an ACF datepicker to organize a list of event posts, and I’d like each event to automatically expire after its datepicker date passes.

    I’ve found a code snippet to do that, and it works, but it’s not quite what I need. Because of the nature of these events it’s important that the posts remain visible for the entire day-of. This snippet automatically expires posts at midnight on the day (so a post for October 3 disappears at 00:00 on October 3).

    Does anybody know a way to tweak this so the posts will expire at midnight on the following day? So a post dated October 3 disappears at 00:00 on October 4?

    I’ve fussed around with adding strtotime all day, but I’m not great with PHP, so I have no idea where to place it semantically.

    The snippet:

    if (!wp_next_scheduled('expire_posts')){
      wp_schedule_event(time(), 'daily', 'expire_posts'); // this can be hourly, twicedaily, or daily
    }
    
    add_action('expire_posts', 'expire_posts_function');
    
    function expire_posts_function() {
    	$today = date('Ymd');
    	$args = array(
    		'post_type' => array('events'), // post types you want to check
    		'posts_per_page' => 50 
    	);
    	$posts = get_posts($args);
    	foreach($posts as $p){
    		$expiredate = get_field('datedate', $p->ID, false, false); // get the raw date from the db
    		if ($expiredate) {
    			if($expiredate <= $today){
    				$postdata = array(
    					'ID' => $p->ID,
    					'post_status' => 'draft'
    				);
    				wp_update_post($postdata);
    			}
    		}
    	}
    }
  • change

    
    if($expiredate <= $today){
    

    to

    
    if($expiredate < $today){
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Expire posts to drafts from datepicker date after 1 day?’ is closed to new replies.