Hide the default listings page to logged out users

Hi!

  1. I want to hide the listings for not logged in users.

I use a plugin to hide gutenberg blocks for logged out users. The problem is that the default listing oage set in the settings isn’t possible to hide. The user can just type Website Hosting - Mysite.com) and still see everything.

Is it possible to hide this content for logged out users and display a text with a login link instead?

  1. I want to hide a certain category in the listings page for logged out users, is this possible?

Thank you for an awesome plugin!

With best regards,

bb

  1. Please try this PHP snippet
add_filter(
	'hivepress/v1/templates/listings_view_page', 
	function($template) {
		if (!is_user_logged_in()){
			wp_redirect(hivepress()->router->get_return_url( 'user_login_page' ));
    		exit;
		}
		
		return $template; 
	},
	1000
);
  1. If the category field is in the listing filter form on the listings page then please try this PHP snippet. Please try to change 1,2,3 on category ids which you want to hide
add_filter(
	'hivepress/v1/forms/listing_filter', 
	function($form) {
		if(is_user_logged_in()){
			return $form;
		}
		
		$category_ids = [1,2,3];
		
		foreach($category_ids as $category_id){
			unset($form['fields']['_category']['options'][$category_id]);	
		}
		
		return $form; 
	},
	1000
);

Hi and thank you for a fast reply!

Number one works perfectly! Thank you so much!

Regarding the second code snippet it doesn’t work. I get this:

Fatal error: Uncaught Error: Cannot unset string offsets in /home/x/public_html/mysite.com/wp-content/themes/listinghive/functions.php:31 Stack trace: #0 /home/x/public_html/mysite.com/wp-includes/class-wp-hook.php(310): {closure}(Array) #1 /home/x/public_html/mysite.com/wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array) #2 /home/x/public_html/mysite.com/wp-content/plugins/hivepress/includes/forms/class-form.php(169): apply_filters(‘hivepress/v1/fo…’, Array, Object(HivePress\Forms\Listing_Filter)) #3 /home/x/public_html/mysite.com/wp-content/plugins/hivepress/includes/forms/class-listing-filter.php(76): HivePress\Forms\Form->__construct(Array) #4 /home/x/public_html/mysite.com/wp-content/plugins/hivepress/includes/helpers.php(279): HivePress\Forms\Listing_Filter->__construct(Array) #5 /home/x/public_html/mysite.com/wp-content/plugins/hivepress/includes/blocks/class-form.php(111): HivePress\Helpers\create_class_instance(‘\HivePress\Form…’, Array) #6 /hom in /home/x/public_html/mysite.com/wp-content/themes/listinghive/functions.php on line 31

Line 31 is: unset($form[‘fields’][‘_category’][‘options’][$category_id]);

Is there a way to fix this? Once again, thank you so much for helping me!

With best regards,

bbb

Please clarify whether you have just put this code snippet and got this error or you have made some changes in it and got this error. If you have made some changes in the code snippet then please share the code snippet which you get after your changes

This code snippet was tested locally and it seems to be ok

Hi made changes to the snippet here:
I changed this: $category_ids = [1,2,3]; to this: $category_ids = [71];

I got the number 71 when I clicked on Listings → Categories → Category name (the URL I’ve pasted further down this post)

I also tried to set $category_ids = [26]; but still the same error.

/wp-admin/term.php?taxonomy=hp_listing_category&tag_ID=71&post_type=hp_listing&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dhp_listing_category%26post_type%3Dhp_listing

Am I doing something wrong?

With best regards,

bbb

This is what I get after the changes:

add_filter(
	'hivepress/v1/forms/listing_filter', 
	function($form) {
		if(is_user_logged_in()){
			return $form;
		}
		
		$category_ids = [71];
		
		foreach($category_ids as $category_id){
			unset($form['fields']['_category']['options'][$category_id]);	
		}
		
		return $form; 
	},
	1000
);

Please disable third-party plugins and customizations (if there are any) and check if this issue persists. It was tested locally and it seems to be ok

If the issue persists, then please send temporary WP access to support@hivepress.io with details for reproducing this issue, and we’ll check it. You can create a temporary access link using this plugin Temporary Login Without Password – WordPress plugin | WordPress.org

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