Skip Navigation

[Resolved] post types view with taxonomy filter AND custom search

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 12 replies, has 3 voices.

Last updated by johnh-18 3 weeks, 2 days ago.

Assigned support staff: Waqar.

Author
Posts
#2167061
toolset.png

Tell us what you are trying to do?
I have created a post types view and applied a taxonomy filter (brand) which works well. But I would like include a custom search filter (tick boxes) that allow me to further filter the selected brand taxonomy by filtering another taxonomy also applied to the same post type.

Is there any documentation that you are following?
I found a few tickets that look similar but not sure that they are identical as they seem to be related to taxonomy views (see attached screenshot). I tried to add the following filter but no success.
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query

Is there a similar example that we can see?
I have added two views to a page
1. the first view is the brand view which I would like to apply a custom search to
2. the second view show all entries in a custom post type and all categories in the taxonomy I want to apply a custom search for.
What I want to do is to have a view that filter per brand and a custom search showing only categories related to that particular brand

#2167205

Hi, let me make sure I understand what you want to achieve here. It looks like you have one View that displays Product posts. That View includes a Query Filter based on the Brand taxonomy, so only Products with some specific Brand term are displayed in the results of this View.

Then, it sounds like you also want to allow the User to select one or more specific categories in a custom search filter. The category options shown in the custom search filter should be limited to only those category terms that applied to these Brand-filtered Products. Is that correct? So the User should select from these Categories in a custom search filter, and the Products results should update to display Products filtered by Brand and Category. The User should not select Brands, those should be selected in the backend using Query Filters. On the front-end, the User should see a Category filter with a limited set of Category options in the filter. The Categories shown in the filter must also be applied to Products having Brand terms that match the Query Filter settings.

If I understand correctly, then you should be able to achieve this with only one View using a combination of Query Filters, front-end filters, and a few specific View configurations. You should probably use the legacy View editor in this case, where you will have more control over the setup options:
- Create a View and choose the Products post type in Content Selection.
- When using the legacy View editor, I suggest you scroll to the top right corner of the screen and open the Screen Options tab. Go ahead and enable "Query Filter", "Search and Pagination," and "Custom Search Settings" here if they are not yet enabled, as well as any other sections you want to be able to edit.
- In the Query Filter panel, add a Query Filter based on the Brand taxonomy. Select the individual Brand term(s) you want to include in the Product results, or choose a more dynamic filter setting like "set by a shortcode attribute" or "set by the current archive page", etc.
- In the Search and Pagination panel, add a new front-end filter control for the Product Category taxonomy. Choose the checkboxes filter type, and leave the other default settings in place. The system will generate the appropriate shortcode for you, and add a corresponding Query Filter automatically.
- In Custom Search Settings for "How do you want to update the results?" choose the option "Let me choose individual settings manually", then set up the filter behavior for AJAX or non-AJAX updates, and whether or not pressing a submit button is required. Then in the section "Which options to display in the form inputs" you must choose the option "Show only available options for each input". This will have the net effect of hiding from the custom search checkboxes any Product Category terms that do not apply to Products filtered by the Brand taxonomy settings you applied in the Query Filter for Brands.
- Build out the View loop as desired, then insert this one View where you want to see the search filter and View results.

#2167231
toolset2.png
toolset3.png

Hi,
Thank you for getting back to me so quickly and your understanding of what I want to do is 100% correct.
I have followed your detailed instructions but there has to be a thing or two that I am not doing right.
The problem I have is that all categories are still listed as opposed to only those related to the selected brand.
A possible reason may be that even if choose "set by the current archive page" in the query filter section, that is automatically changed to 'Filter based on the frontend search filter by Shopping categories.' as soon as I add the filter In the 'search and pagination' panel - see attached screenshots. Great if you can help.
Enjoy the rest of the Sunday / jan

#2167491
view-loop.png
view-search-pagination.png
view-in-wp-archive.png
view-default-in-archive-foo.png
view-default-in-archive-bar.png
view-default-in-archive-baz.png
Screen Shot 2021-09-12 at 8.56.26 PM.png
Screen Shot 2021-09-12 at 8.56.38 PM.png

A possible reason may be that even if choose "set by the current archive page" in the query filter section, that is automatically changed to 'Filter based on the frontend search filter by Shopping categories.' as soon as I add the filter In the 'search and pagination' panel - see attached screenshots.
Okay the Shopping Categories query filter should update automatically when you insert the filter for the Shopping Categories in the search and pagination panel, but there should also be a Brands taxonomy Query Filter here. I don't see one, so I'm not sure if you're saying you created it but it was deleted somehow or if there was something else going on.

- I see one Query Filter for the Shopping Categories taxonomy (looks like the taxonomy slug is concept-store). This Query Filter was most likely created for you automatically when you inserted a filter control in the Search and Pagination editor panel, unless you had already created one manually. In that case, it would have been updated automatically based on the filter control in the Search and Pagination panel. I would not try to configure the Query Filter corresponding to a custom search filter manually unless you're really sure you understand all the settings. The URL parameters, slugs and other settings must all match perfectly with the settings in the custom search filter control. So I would leave this Query Filter as it is currently set, I suspect it's configured correctly to function with the Shopping Categories filter control.

- There should be one more taxonomy term Query Filter configured here for the Brand taxonomy, but I don't see one. Normally it would look similar to the two screenshots 2021-09-12 at 8.56.xx I've attached here, showing two taxonomy Query Filters. The Brands taxonomy term Query Filter must be added and configured manually, so click "Add a Filter" below the existing Shopping Categories Query Filter. Choose a taxonomy term Query Filter for the Brands taxonomy. This taxonomy term filter should be configured as "set by the current archive page". The Brand taxonomy term Query Filter should not be impacted when you manipulate the configurations for the Shopping Category filter settings, and vice versa. It should remain just as you configure it manually. If you're saying that you had already added the Brands Query Filter but it was somehow deleted when you inserted the Shopping Category filter control in Search and Pagination, that is unusual...I would not expect that behavior unless there was a Brands filter control as well in the Search and Pagination panel, and you deleted that control while cleaning up some extra code in there. In that case, you would have been prompted to delete the corresponding Brand taxonomy term Query Filter since the filter control was deleted manually - the system would assume that the Brand taxonomy term Query Filter was no longer necessary. But in your case we will still require the Query Filter.

I suggest you add the Brands taxonomy term Query Filter manually as described above and check it a few times while you continue to edit this View. If the Brands Query Filter disappears or is reconfigured somehow without your direct control, that is something we can investigate further.

I set up a similar example View with two Query Filters - one for taxonomy A (Product Additional Taxes in my example) set by the current archive page, and another for taxonomy B (Product Categories in my example) using a custom search filter. The "Show only available options for each input" option is turned on for this custom search View, and the View is placed in a WordPress Archive for taxonomy A. I've attached screenshots showing the View configurations, the WordPress Archive in the Block Editor, and the front-end default checkboxes options on 3 different Tax A term archives. You can see in each case, the available options for the Product Category filter are different, depending on the results shown in the View. In the loop of the View I've included a link to the post and the post's terms from both taxonomies.

Let me know if you have questions about this.

#2167941

Hi Christian,
Many thanks. You are right - I created the brands taxonomy query filter but it was deleted along with 'set by the current archive page'. However, I added it again as per your instructions and all is working as it should which is great - many thanks.
A final question - the 'concept-store' taxonomy has three different levels, eg
- Beauty & Wellbeing
- - Skincare
- - - serum
is it possible to change the parameters in the 'search and pagination' panel so that only level 3 (or level 2 and 3) are included?
Best regards / jan

#2168419

is it possible to change the parameters in the 'search and pagination' panel so that only level 3 (or level 2 and 3) are included?
No, unfortunately there are no configurations available in the View editor that will help filter the options by hierarchical taxonomy level. We have some custom code snippets available that are intended to help filter taxonomy term options in a custom search filter, but I'm not sure if they will work with the scenario you have here. Let me run some tests and I will give you an update tomorrow as soon as possible when my shift begins. I'll let you know what I find out.

#2168643

Good morning Christian,
Thanks for offering to check if there might be a solution. I have lots of other things to play with so whenever it suits you.
Have a nice day / jan

#2174411

Hi, sorry for the delay. I wanted to report that I haven't been able to get the results I was hoping for. It seems that custom code snippet I have available to blacklist specific filter terms, and the View configuration to "Show only available options for each input", are a bit incompatible. The initial set of filter options is correct when the page loads, but as the User selects some checkboxes, the other checkbox options update automatically to show only those checkboxes that would produce results when checked, along with the currently checked options. That automatic option update process does not seem to respect the blacklist filter, unfortunately. So AJAX updates to the filter options are not working as desired, because blacklisted terms appear in the filter options at that point.

With AJAX updates disabled and a Submit button added to the search form, the snippet seemed to work fairly well in my local tests. Let me know if you'd like to try the snippet with AJAX updates disabled.

#2176625

Hi Christian,
Yes please - what do I need to do?
Best regards / jan

#2177489

After a bit more investigation, I think I have a solution that works with AJAX updates. I had previously overlooked one of the newer filters that is available in Blocks/Views, and it is designed to hook into these taxonomy term filter changes with or without AJAX updates. Here is the custom code I currently have implemented on my site:

// https://toolset.com/forums/topic/post-types-view-with-taxonomy-filter-and-custom-search/
// Utility function to get the ids of all top-level terms in a taxonomy
// returns an array of integers
// you should not edit this function at all
function tssupp_get_top_level_terms($tax_slug) {
  $terms = get_terms(
    array(
      'taxonomy' => $tax_slug,
      'parent' => 0,
      'fields' => 'ids',
    )
  );
  return is_wp_error($terms) ? array() : $terms;
}

// https://toolset.com/forums/topic/post-types-view-with-taxonomy-filter-and-custom-search/
// filter to remove top-level terms from a custom search taxonomy filter
// this code is for use in a taxonomy WP archive (not the filter taxonomy, any other taxonomy)
add_filter( 'wpv_filter_taxonomy_frontend_search_get_terms_args', 'tssupp_modify_get_terms_args', 10, 3 );
function tssupp_modify_get_terms_args( $args, $taxonomy, $view_id ) {

  $views = array( 548 ); // View with tax filter we wish to modify
  $tax_slug = 'product_cat'; // Slug of the taxonomy whose WP Archive has the search filter 

  // you should not edit below this line

  if ( in_array( $view_id, $views ) ){
    $to_exclude = tssupp_get_top_level_terms( $tax_slug );
    $args['exclude'] = $to_exclude;
  }

  return $args;

}

The first bit of code is a utility function that will help get all the IDs of top-level terms in any custom taxonomy. You should not need to edit anything in this function. The next chunk of code hooks into a Views Filter wpv_filter_taxonomy_frontend_search_get_terms_args, which adjusts the arguments for the get_terms call used to populate the custom search filter. You should change 548 to match the ID of the custom search View, and change product_cat to match the slug of the WP Archive taxonomy (in your case this is not concept-store, but the other taxonomy, whose WP Archive has the concept-store taxonomy filter).

Please add this code to your child theme's functions.php file, or append to the end of a new custom code snippet in Toolset > Settings > Custom Code. Set the snippet to run everywhere and activate it.

Once that is in place, review the results on the front-end of the site. If you're not seeing what you expect, I'll need to take a look in wp-admin to give you more direct guidance. I am activating private reply fields here so you can share login credentials in your next reply if necessary.

Views Filter documentation for wpv_filter_taxonomy_frontend_search_get_terms_args:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_frontend_search_get_terms_args

#2180365
ts2.jpg
ts1.jpg

Hi Christian,
I have been away a couple of days so so need to look at this in more detail but did a quick attempt to implement the code which generated an error message. Give me a day or two and I will get back to you. Two questions though -
1. You write 'You should change 548 to match the ID of the custom search View' - I assume this is the ID you are referring to - see attachment ts1
2. You write 'change product_cat to match the slug of the WP Archive taxonomy (in your case this is not concept-store, but the other taxonomy, whose WP Archive has the concept-store taxonomy filter)' - I am not certain I fully understand and that may well be why it does not work - the slug I used in my very quick trial was in this case /beauty-wellbeing/ - see attachm ts2. I suspect however that this is not what you meant as the view should work for a range of categories.
I am happy to continue trying to make it work but great if you could clarify the 2 questions above. Alternatively, if you think it is easier / faster for you to do the implementation direct, that is of course OK as well - whatever works best for you.
Best regards / jan

#2181603

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thanks for writing back.

Christian won't be able to follow up, so I'll be assisting you on this ticket.

> 1. You write 'You should change 548 to match the ID of the custom search View' -
> I assume this is the ID you are referring to - see attachment ts1

- Your understanding is correct and the screenshot shows the view's ID, that needs to be used.

> 2. You write 'change product_cat to match the slug of the WP Archive taxonomy
> (in your case this is not concept-store, but the other taxonomy, whose WP Archive
> has the concept-store taxonomy filter)'

- I've read this thread a couple of times and also performed some tests on my website and it is my understanding that you'll need to replace "product_cat" with "concept-store" in the code snippet, as that is the taxonomy whose terms needs filtering.

I'm not sure 100% why Christian recommended otherwise, but, you're welcome to test the code with "concept-store" and let me know how it goes.

regards,
Waqar

#2184725

My issue is resolved now. Thank you!