How to expire a listing with the date attribute given by the user

I want to the listing dissapear from listings page exacly after the date (day hour and minute) attribute that the user selects, how to do this?

1 Like

Please try this PHP snippet

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {
		$form['fields']['expired_time'] = [
				'label'       => 'custom label',
				'description' => 'custom description',
				'type'        => 'date',
				'format'      => 'U',
				'_order'      => 123,
			];

		return $form;
	},
	1000
);
1 Like

Hi yevhen, I’m trying your solution but I got another question, how to show this attribute in only one category?

Please try this PHP snippet instead. Please just change 1,2,3 on categories ids where you want to add this field

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form_args, $form ) {
		$listing = $form->get_model();
		
		if ( $listing && array_intersect( $listing->get_categories__id(), [ 1,2,3 ] ) ) {
			$form_args['fields']['expired_time'] = [
				'label'       => 'custom label',
				'description' => 'custom description',
				'type'        => 'date',
				'format'      => 'U',
				'_order'      => 123,
			];
		}

		return $form_args;
	},
	1000,
	2
);
1 Like

almost done, how to select date, hour, minute and make this attribute required??

Please try this PHP snippet instead. Please just change 1,2,3 on categories ids where you want to add this field

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form_args, $form ) {
		$listing = $form->get_model();
		
		if ( $listing && array_intersect( $listing->get_categories__id(), [ 1,2,3 ] ) ) {
			$form_args['fields']['expired_time'] = [
				'label'       => 'custom label',
				'description' => 'custom description',
				'type'        => 'date',
				'time' => true,
				'required' => true,
				'format'      => 'U',
				'_order'      => 123,
			];
		}

		return $form_args;
	},
	1000,
	2
);

I forgot one thing: how to display this date atribbute on page secondary and block secondary with a calendar icon?

Unfortunately, there’s no simple code snippet - this requires advanced customizations

there is a way to contour this problem? Like instead of creating a custom attribute with php code, create a regular date attribute with day and hour display it on page and block secondary and then edit to make the listing expires on this date attribute?

Please try to add a custom listing date attribute with a field name, for example, custom_expired_time (it is important not to use expired_time as a field name as it will cause problems with saving data) and label, for example, Expired time. So, you can set the area where you want to show this attribute and also the attribute icon. Then please try to use this PHP snippet instead of the previous one. But please note that if you set another field name for attribute then it is needed to change get_custom_expired_time on get_your_attribute_field_name in the code snippet

add_action(
	'hivepress/v1/models/listing/update',
	function( $listing_id, $listing ) {
		if ( $listing->get_custom_expired_time() ) {
			update_post_meta($listing_id, 'hp_expired_time', strtotime($listing->get_custom_expired_time()));
		}
	},
	10,
	2
);

so I’ll need to create a custom attribute on wordpress > listings > attributes > Add new with a field name custom_expired_time? but how to change label? this way its only possible to edit field name not label

or using a two snippet code like:

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form_args, $form ) {
		$listing = $form->get_model();
		
		if ( $listing && array_intersect( $listing->get_categories__id(), [ 1,2,3 ] ) ) {
			$form_args['fields']['custom_expired_time'] = [
				'label'       => 'Expired time',
				'description' => 'custom description',
				'type'        => 'date',
				'time' => true,
				'required' => true,
				'format'      => 'U',
				'_order'      => 123,
			];
		}

		return $form_args;
	},
	1000,
	2
);

and then using this last:

add_action(
	'hivepress/v1/models/listing/update',
	function( $listing_id, $listing ) {
		if ( $listing->get_custom_expired_time() ) {
			update_post_meta($listing_id, 'hp_expired_time', strtotime($listing->get_custom_expired_time()));
		}
	},
	10,
	2
);

Yes, please create a new listing attribute with the label Expired time (but you can set any label) and field name/slug custom_expired_time (you can set any field name/slug for this attribute but please note that if you set another field name for attribute then it is needed to change get_custom_expired_time on get_your_attribute_field_name in the code snippet). Set attribute area, icon, etc. Then please try to use only this PHP snippet

add_action(
	'hivepress/v1/models/listing/update',
	function( $listing_id, $listing ) {
		if ( $listing->get_custom_expired_time() ) {
			update_post_meta($listing_id, 'hp_expired_time', strtotime($listing->get_custom_expired_time()));
		}
	},
	10,
	2
);

My doubt is how to create this attribute: with front end listing>attributes>add new or with php code?

1- create a date atrribute on listing>attributes>add new
2- set field name “date”
3- add this snippet code:

add_action(
	'hivepress/v1/models/listing/update',
	function( $listing_id, $listing ) {
		if ( $listing->get_date() ) {
			update_post_meta($listing_id, 'hp_expired_time', strtotime($listing->get_date()));
		}
	},
	10,
	2
);

this way?

Yes, but setting date as the field name can cause problems, it is needed testing. custom_expired_date should be ok as the field name

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