Skip to content

Instantly share code, notes, and snippets.

@lukecav
Created November 11, 2016 21:45
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lukecav/39e0407e082aa5cd3cdbf90b2e9e036b to your computer and use it in GitHub Desktop.
Save lukecav/39e0407e082aa5cd3cdbf90b2e9e036b to your computer and use it in GitHub Desktop.
Expire posts to draft using a datepicker field in ACF
// 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);
}
}
}
@yvettejolie
Copy link

yvettejolie commented Sep 6, 2020

Hi Luke,

First of all, great piece of code! Really does what I need it to do.
Having said that, in debug mode the 2nd line of this code gives me some errors.
Notice: Undefined variable: post and Notice: Trying to get property of non-object.

I was wondering if you could tell me how to get rid of this error?
I just don’t understand why it’s giving me an error while the code works.
Trying to wrap the whole thing in a function and then adding global $post; at the start. This makes the errors go away, but then the code doesn’t do its thing anymore.

Looking forward to hearing from you!

Kind regards,
Yvette Jolie

@rmollema
Copy link

Exactly wat i needed. Thanks Luke! Works like a charm

@lukecav
Copy link
Author

lukecav commented Dec 11, 2020

Your welcome.

@jaron1231
Copy link

Thanks so much for sharing! Quick question though, since I am a Wordpress newb:

I have to name the field for the expiry datepicker 'end_date_time' right?

And this function does not draft events that have happened already right? Since it compares it to 'today's date'

@lukecav
Copy link
Author

lukecav commented Dec 15, 2020

Yep correct.

@ubelisrle
Copy link

ubelisrle commented Jul 9, 2021

Great code!
Do i need to change 1 * DAY_IN_SECONDS If i want to check date daily?

This is my code:

if ($expireTransient = get_transient($post->ID) === false) {
	set_transient($post->ID, 'set for 1 days', 1 * DAY_IN_SECONDS );
	$today = date('Ymd');
	$args = array(
		'post_type' => 'b2bking_rule',
		'posts_per_page' => -1,
		'post_status' => 'publish',
		'meta_query' => array(
			array(
				'key' => 'do_datuma',
				'value' => $today,
				'compare' => '<='
			)
		)
	);
	$posts = get_posts($args);
	foreach( $posts as $post ) {
		if(get_field('do_datuma', $post->ID)) {
			$postdata = array(
				'ID' => $post->ID,
				'post_status' => 'draft'
			);
			wp_update_post($postdata);
		}
	}
}

Thanks!

@lukecav
Copy link
Author

lukecav commented Jul 9, 2021

@ubelisrle
Copy link

Great! Thanks!

@aaynaic
Copy link

aaynaic commented Jul 12, 2021

How would I use the code if I wish to expire multiple post types?

@WPJON
Copy link

WPJON commented Nov 2, 2021

This is great - works like a charm - Anyway we can change the post category as well? - Thanks!

@lukecav
Copy link
Author

lukecav commented Nov 2, 2021

Should be possible.

@WPJON
Copy link

WPJON commented Nov 4, 2021

Looks like I got the category to change aswell: But shouldn't this be wrapped into a function? I feel this would hammer the server no?

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' => 'post',
		'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' => 'publish'
			);

                        // Below changes the category of the post. 
                        
                        $post_id = $post->ID;
                  
                        wp_set_post_categories( $post_id, array(13), $append ); // id of category in array. 
                            
                        wp_update_post($postdata); 
		 }
	}
}

@lukecav
Copy link
Author

lukecav commented Nov 4, 2021

Go wrap it in a function and change the transient to expire later if needed.

@sarthaktec
Copy link

Hi Luke,

First of all, great piece of code! Really does what I need it to do.
Having said that, in debug mode the 2nd line of this code gives me some errors.

Notice: Undefined variable: post and Notice: Trying to get property of non-object.

I was wondering if you could tell me how to get rid of this error?
I just don’t understand why it’s giving me an error while the code works.
Trying to wrap the whole thing in a function and then adding global $post; at the start. This makes the errors go away, but then the code doesn’t do its thing anymore.

Looking forward to hearing from you!

Kind regards,
Sarthak Priyadarshan

@craigcallen
Copy link

Thank you for this code, I use it often. Most of my sites I host on Siteground and have no issue, but for some reason this will not work on AWS Lightsail or EC2. Would you have any advice or idea why that might be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment