Support

Account

Home Forums General Issues Expire post to draft

Solving

Expire post to draft

  • Is it possible to expire a post to a draft when it reaches it’s expire date from your date picker?

  • You could just create a function that loops through all your posts and checks the time, and if expired updates the post status to draft. Run it on cron to keep it from killing performance.

    if (!wp_next_scheduled('expire_posts')){
      wp_schedule_event(time(), 'daily', 'expire_posts');
    }
    
    add_action('expire_posts', 'expire_posts_function');
    
    function expire_posts_function() {
    	$today = time();
    	$args = array(
    		'post_type' => array('post'), // array of the post types you want to check
    		'posts_per_page' => -1 // get all the posts
    	);
    	$posts = get_posts($args);
    	foreach($posts as $p){
    		if(get_field('expiration', $p->ID) > $today){
    			$postdata = array(
    				'ID' => $p->ID,
    				'post_status' => 'draft'
    			);
    			wp_update_post($postdata);
    		}
    	}
    }
  • Did anybody do this successfully? I’m trying to expire post to draft with date from date field (format DD.MM.YYYY) but so far without success. Must I convert date field value to unix timestamp maybe?

    Regards

  • Hi @klox7

    The time() function returned Unix timestamp, so yes, you need to convert the date from the custom field to Unix timestamp too.

    Hope this helps.

  • I’m interested in the same thing but I am wondering where in the Cron are you supposed to put this?

    Also, how do you convert the date from the custom field to Unix timestamp?

  • This reply has been marked as private.
  • Was this resolved as I am looking for the same thing.

    Thanks

  • Hi @huwrowlands

    You can always query the post by the date. That way, you can loop trough the returned posts and set them as a draft. To learn more about the query, please take a look at this page: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/.

    Don’t forget to execute it using the cron job like Wstaley said. This page should give you more idea about it: https://www.smashingmagazine.com/2013/10/schedule-events-using-wordpress-cron/.

    Thanks ๐Ÿ™‚

  • Might could streamline the code a bit, but this is working for me. Hope it helps.

    // Expire events
    if ($expireTransient = get_transient($post->ID) === false) {
    	set_transient($post->ID, 'set for 1 minutes', 1 * MINUTE_IN_SECONDS );
    	$today = date('Y-m-d H:i:s', current_time('timestamp', 0));
    	$args = array(
    		'post_type' => 'events',
    		'posts_per_page' => 200,
    		'post_status' => 'publish',
    		'meta_query' => array(
    			array(
    				'key' => 'end_date_time',
    				'value' => $today,
    				'compare' => '<='
    			)
    		)
    	);
    	$posts = get_posts($args);
    	foreach( $posts as $post ) {
    		if(get_field('end_date_time', $post->ID)) {
    			$postdata = array(
    				'ID' => $post->ID,
    				'post_status' => 'draft'
    			);
    			wp_update_post($postdata);
    		}
    	}
    }
  • Just wanted to add that wstaley’s solution worked great for me, but you need to change his greater-than symbol to less-than. I also am not using the time of day, only the date, for expiration, so I changed time() to date().

    Here is my working code:

    // expire offer posts on date field.
    if (!wp_next_scheduled('expire_posts')){
      wp_schedule_event(time(), 'twicedaily', '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('offer'), // post types you want to check
    		'posts_per_page' => -1 
    	);
    	$posts = get_posts($args);
    	foreach($posts as $p){
    		$expiredate = get_field('valid_end_date', $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);
    			}
    		}
    	}
    }
  • This is exactly what I’m looking for!
    How would I modify it to apply to WooCommerce products?

    Thanks!

  • Webtech, you should just have to change the post type to “product” or whatever woocommerce’s product post type is named.

    $args = array(
    	'post_type' => array('product'), // post types you want to check
    	'posts_per_page' => -1 
    );
  • I originally thought that but figured it was way to simple to work lol, thanks!

  • Hi all, the field I’m using is a date/time picker, pretty much restricted with this, and when I use the function it drafts everything no matter if it is pass due or not. I’m assuming the format of the date/time field is not appropriate for this function.

    Since the time() function returns seconds since the Unix Epoch I guess it need to convert the $expiredate into seconds to compare the two or convert what the time() returns into mm/d/yyyy h:mm AM/PM? Or am I completely off track here?

    Thanks!

  • @lsterling03 codes work fine, but do I need to still set up the CRON job?

  • Hi, can someone explain how this works? I’m using code from @lsterling03 , but it doesn’t works for me. I can share my date-picker field settings, if needed…

Viewing 16 posts - 1 through 16 (of 16 total)

You must be logged in to reply to this topic.