# Allow restricting listing attribute fields by package

**URL:** <https://community.hivepress.io/t/allow-restricting-listing-attribute-fields-by-package/15071>\
**Category:** Paid Listings\
**Tags:** code\
**Created:** [February 20, 2025, 11:30pm UTC](https://community.hivepress.io/t/allow-restricting-listing-attribute-fields-by-package/15071 "2025-02-20T23:30:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [February 20, 2025, 11:30pm UTC](https://community.hivepress.io/t/allow-restricting-listing-attribute-fields-by-package/15071/1 "2025-02-20T23:30:59Z")

</div>

The snippet below allows selecting restricted listing attributes per package (by selecting them in the package settings). These attribute fields are then not available for editing unless the corresponding listing package is selected/purchased. This is a temporary solution until we add this functionality to the next major Memberships update.

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [February 20, 2025, 11:31pm UTC](https://community.hivepress.io/t/allow-restricting-listing-attribute-fields-by-package/15071/2 "2025-02-20T23:31:52Z")

</div>

```auto
add_filter( 'hivepress/v1/meta_boxes/listing_package_settings', 'hp_add_package_fields', 1000 );
add_filter( 'hivepress/v1/models/listing_package', 'hp_add_package_fields', 1000 );
add_filter( 'hivepress/v1/models/user_listing_package', 'hp_add_package_fields', 1000 );

function hp_add_package_fields( $form ) {
	$attributes = array_filter( wp_list_pluck( hivepress()->attribute->get_attributes( 'listing' ), 'label' ) );

	$form['fields']['attributes'] = [
		'label' => 'Attributes',
		'type' => 'select',
		'options' => $attributes,
		'multiple' => true,
		'_external' => true,
		'_order' => 100,
	];

	return $form;
}

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {
		if ( current_user_can( 'edit_others_posts' ) ) {
			return $form;
		}

		$packages = \HivePress\Models\Listing_Package::query()->filter(
			[
				'status' => 'publish',
			]
		)->get();

		$package_attributes = [];

		foreach ( $packages as $package ) {
			$package_attributes = array_merge( $package_attributes, (array) $package->get_attributes() );
		}

		$package_attributes = array_unique( $package_attributes );

		$user_packages = \HivePress\Models\User_Listing_Package::query()->filter(
			[
				'user' => get_current_user_id(),
			]
		)->get();

		$user_attributes = [];

		foreach ( $user_packages as $package ) {
			$user_attributes = array_merge( $user_attributes, (array) $package->get_attributes() );
		}

		$user_attributes = array_unique( $user_attributes );

		foreach ( $package_attributes as $attribute ) {
			if ( ! in_array( $attribute, $user_attributes ) ) {
				unset( $form['fields'][$attribute] );
			}
		}

		return $form;
	},
	1000
);

add_filter(
	'hivepress/v1/menus/listing_submit',
	function( $menu ) {
		if ( isset( $menu['items']['listing_submit_package'] ) ) {
			$menu['items']['listing_submit_package']['_order'] = 15;
		}

		return $menu;

	},
	1000
);

add_filter(
	'hivepress/v1/templates/listing_submit_package_page',
	function( $template ) {
		hivepress()->template->fetch_block( $template, 'listing_details_change_link' );

		return $template;

	},
	1000
);

```

---

<div class="post-metadata">

**Author:** ![system](https://community.hivepress.io/user_avatar/community.hivepress.io/system/32/13990_2.png) [@system](https://community.hivepress.io/u/system)\
**Post date:** [March 22, 2025, 11:32pm UTC](https://community.hivepress.io/t/allow-restricting-listing-attribute-fields-by-package/15071/3 "2025-03-22T23:32:15Z")

</div>

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