Creating an attribute with a PHP snippet

Hello everyone,
I used the code below and did not work, actually it works… the selected countries get saved on the listing but when I want to show them on the block or the page they dont appear, it doesnt work either if I want to use it as a filter, the filter says “no results found”

thank you!

add_filter(
	'hivepress/v1/models/listing/attributes',
	function ($attributes){
		if(isset($attributes['targeted_countries'])){
			$attributes['targeted_countries']['edit_field']['options'] = [
				'1'   => 'One',
				'2'   => '2',
				'3'   => '3',


			];
			$attributes['targeted_countries']['edit_field']['option_args'] = [];
		}
		
		return $attributes;
	},
	1000
);

Hi,

This code is correct, but it will not display the value on the listing page, also in the filter or search, for this you need to modify this code and add the display area, fitlrable, etc. I recommend using attribute listing for this, there is no coding needed and you can mark the attribute as filtrable and searchable, please check this doc How to add listing attributes - HivePress Help Center

thank you for your reply,
I dont want the traditional way because it will be a “Choose country” select so I will have to add each country…
With the manual way I only have copy and paste the country list…

tried this but didnt work…:

add_filter(
	'hivepress/v1/models/listing/attributes',
	function ($attributes){
		if(isset($attributes['test'])){
			$attributes['test']['edit_field']['options'] = [
				'500' => 'Peru',
				'501' => 'Mexico',
				'503' => 'Cuba',
				'504' => 'All',
			];
			$attributes['test']['filterable'] = true;
			$attributes['test']['display_areas'] = ['view_page_secondary'];
		}
		
		return $attributes;
	},
	1000
);

I would really appreciate if you point me to the right direction

Thank you for waiting

Please check this example if you want to create a custom attribute with the code.

For your case, please check these instructions.

The editable parameter is required to make attributes editable in forms.

The filterable parameter is required if you want to show attributes in the filter form of some model (listing, vendor, etc.). Also, there can be sortable or searchable parameters to set to sort or search forms for some models.

edit_field parameters are required if you want to set the general structure of the attribute. It defines that this attribute contains text, number, or another type of content.

search_field parameters are required to set the attribute type for a filter, search, or sort form.

The _external parameter is required if you want to create the independent from some model selectable options for the attribute. So this parameter gives you complete control of selectable attribute options.

$attributes['test_attribute'] = [
			'editable'      => true,
			'filterable'    => true,

			'display_areas' => [
				'view_page_secondary',
			],

			'edit_field'    => [
				'label'     => 'Test attribute',
				'type'      => 'select',
				'options'   => [
					'test_1' => 'Test 1',
					'test_2' => 'Test 2',
					'test_3' => 'Test 3',
				],
				'_external' => true,
				'_order'    => 30,
			],

			'search_field'    => [
				'label'     => 'Test attribute',
				'type'      => 'select',
				'options'   => [
					'test_1' => 'Test 1',
					'test_2' => 'Test 2',
					'test_3' => 'Test 3',
				],
				'_external' => true,
				'_order'    => 30,
			],
		];

I hope this will be useful for you. Also, you can check our Developer Docs or Code Snippets collection for additional information or examples

2 Likes

Perfect! Exactly what I needed.

Thank you very much!

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