Show all sub categories in filter sidebar by default

When using sub categories, only the top level categories are shown in the filter sidebar at first. Sub categories are only shown, after filtering the parent category. The problem is here that users don’t know if and if so, which sub categories will be displayed. This makes it a hard time filtering when using sub categories.

Is there a way to show the entire categories tree by default? So that all sub categories are displayed too?

Happy to code this in my functions.php, but would need a snippet or pointer to start with …

Thanks heaps!

Sorry, there’s no simple code snippet for this, it would require a custom implementation. If customizations beyond the available features are required for your site, please consider hiring someone for custom work https://fvrr.co/32e7LvY

Ok, thanks for your quick reply. Could you point me to the right place in the code or the general approach you would suggest? Happy to create my own templates or alter code in hivepress, but it would be helpful to know where to start looking :slight_smile: THANKS!!

Yes, if you’re familiar with PHP please try using the hivepress/v1/forms/listing_filter hook to filter the form parameters, this way you can override parameters for the _category field and replace or extend its options.

1 Like

Hi ihor, Thanks for the pointer – This is already super helpful!

Thanks again for the pointer ihor. I didn’t manage to code it as orignally intended, but I found a way to show a pre-defined list of categories (names and IDs of categories are hardcoded in this solution), independent of the active (current) category. This way it will always be the full list of categories that is shown and not just the one that the user has clicked on.

I also added an additional css class as an attribute to be able to specifically target the category list and custom style it.

The (hardcoded) approach is not really elegant, but it might help someone else nevertheless:

add_filter(
	'hivepress/v1/forms/listing_filter',
	function( $form_args, $form ) {
		
		if(isset($form_args['fields']['_category'])){
			$form_args['fields']['_category'] = array_merge(
				$form_args['fields']['_category'],
				[
					'label' => 'Category Label',
					'required' => 'false',
					'type' => 'radio',
					'options' => [
						'0' => 'All Categories', '22' => 'Category Name One', '30' => 'Category Name Two', '25' => 'Category Name Three', '29' => 'Category Name Four',
					],
					'attributes' => [
						'class' => [
							'filter_categories',
						]
					],
				]
			);
		}
		return $form_args;
	},
	1000,
	2
);
1 Like

Can I see your website to see how you got the subcategories to work?

Actually, I didn’t use sub categories. All categories are on the same logical level, but the (pseudo) sub categories are indented by 30 px or so from the left. So I get a tree like look. My main aim was to have the full tree expanded at any time, no matter what. The default behaviour of hivepress is to only show the current category and hide all others. For my purpose this wasn’t the right thing. Therefore I hardcoded all categories in the filter “hivepress/v1/forms/listing_filter”.

The pseudo “sub” categories are targeted by CSS to achieve the indent (margin-left). This is also the reason for the “attributes” array in the code above. This adds the class “filter_categories” to the output which can then be targeted by CSS. It’s not a very elegant solution but it works for me. I only have a few categories and they are pretty permanent, therefore this hardcoded solution is good enough for my purpose.

Here’s a screenshot of the categories filter:

2 Likes

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