Skip Navigation

[Gelöst] Post Expiry

This support ticket is created vor 1 Jahr, 5 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 Antworten, has 2 Stimmen.

Last updated by lindsayH vor 1 Jahr, 5 Monate.

Assisted by: Waqar.

Author
Artikel
#2482319

I know this may be outside your support remit but thought worth an ask.

I've created PHP code to look at a custom field in my CPT Events and if that event date is less than today's date to change the post status. It doesn’t work. I'm not sure if it's to to with the date format, I know you store in UTC format.

If you can see any obvious mistakes I'd be very grateful if you can highlight those. I'm not a PHP expert by any means.

wp_schedule_event(time(), 'hourly', 'my_hourly_event');
//add_action('init','my_hourly_event');
function my_hourly_event() {
	$the_query = get_posts( 'post_type=event' );	
	foreach($the_query as $single_post) {
		$id=$single_post->ID;
		$event_date=get_post_meta($id, 'wpcf-event-date', true );
		if($event_date!=''){
			$today = strtotime('today');
			// $today=date("F j, Y");
			if($event_date<$today){
				$update_post = array(
				'ID' 			=> $id,
				'post_status'	=>	'private',
				'post_type'	=>	'event' );
				wp_update_post($update_post);
			}	
		}
	}
}

Thank you for your time.

#2482539

Waqar
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

I've performed some tests and was able to make your custom code work on my test website, after slight adjustments:


// register the recurring event
if (! wp_next_scheduled ( 'my_hourly_event' )) {
	wp_schedule_event(time(), 'hourly', 'my_hourly_event');
}

// register the action to execute when the recurring event takes place
add_action( 'my_hourly_event', 'my_hourly_event_func' );
function my_hourly_event_func() {
	$the_query = get_posts( 'post_type=event' );    
	foreach($the_query as $single_post) {
		$id=$single_post->ID;
		$event_date=get_post_meta($id, 'wpcf-event-date', true );
		if($event_date!=''){
			$today = strtotime('today');
			if($event_date<$today){
				$update_post = array(
					'ID'            => $id,
					'post_status'   =>   'private',
					'post_type' =>   'event' );
				wp_update_post($update_post);
			}
		}
	}
}

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2482889

Thank you so much for this outstanding help.

I'm testing (I'm away at the moment). I'll report back here if it works for me, mainly because the code could be useful for someone else too.

#2483023

Waqar
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Glad I could help.

Please take your time and let us know how it goes.

#2483217

My issue is resolved now.

The code works perfectly.

Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.