I'd like a listing to expire based on a custom date attribute

I have created a custom attribute called Event End Date that the user defines. I would like their listing to expire and be removed from the listings page one day after this date. I am using the below snippet, but it isn’t working:

add_action(
	'hivepress/v1/models/listing/update',
	'custom_update_expiration_date',
	10,
	2
);

function custom_update_expiration_date($listing_id){
	remove_action( 'hivepress/v1/models/listing/update', 'custom_update_expiration_date', 10, 2 );
	
	$listing = \HivePress\Models\Listing::query()->get_by_id($listing_id);
	
	if(!$listing){
		return;
	}
	
	$end_time = $listing->get_attribute_field_name();
		
	if(!$end_time){
		return;
	}
		
	$listing->set_expired_time(strtotime('+2 days', strtotime($end_time)))->save_expired_time();
}

Please try this PHP code snippet. This code snippet uses this action hook Action: hivepress/v1/models/{model_name}/update_{field_name} | HivePress Hook Reference, which will update the Expiration date attribute of the listing only when your custom attribute updates.

add_action(
	'hivepress/v1/models/listing/update_attribute_field_name',
	function($listing_id, $value){
		$listing = \HivePress\Models\Listing::query()->get_by_id($listing_id);
		
		if(!$listing){
			return;
		}
		
		$listing->set_expired_time(strtotime($value.' + 1day'))->save_expired_time();
	},
	1000,
	2
);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.