Restricting all listing in a particular category

Hi,
I have purchased membership extension. My primary reason is to restrict the access to all listing in a particular category.

Sorry for the delay.

You can try using this code snippet, replace 1,2,3 with IDs of the listing categories you want to restrict access to. Also, make sure that All Pages restriction is selected for listings in HivePress/Settings/Memberships.

add_action(
	'template_redirect',
	function () {
		$category_id = 0;

		if ( is_tax( 'hp_listing_category' ) ) {
			$category_id = get_queried_object_id();
		} elseif ( is_search() && get_post_type() === 'hp_listing' ) {
			$category_id = absint( hivepress()->helper->get_array_value( $_GET, '_category' ) );
		} elseif(is_singular('hp_listing')) {
			$category_id=absint(hivepress()->helper->get_first_array_value(wp_get_post_terms(get_the_ID(), 'hp_listing_category', ['fields' => 'ids'])));
		}

		if ( ! in_array( $category_id, [ 1,2,3 ] ) ) {
			hivepress()->request->set_context( 'membership_plan', null );
		}
	},
	1
);

Please note that it may require further customizations.

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