Skip Navigation

[Resolved] Group search results by category/taxonomy

This thread is resolved. Here is a description of the problem and solution.

Problem: I have 4 categories: articles, video, audio, events.
I am searching for free text "basketball".
I want the results to show:

Results for "basketball" under "articles":
article-1, article-2, article 3

Results for "basketball" under "videos":
video-1, video-2, video-3

Results for "basketball" under "audio":
audio-1, audio-2, audio-3

Results for "basketball" under "events":
event-1, event-2, event-3

Solution:
There is not a good way to split up a single custom search View this way. Instead, you could use 4 separate custom search Views, each filtered by a single term. Then you can use conditional HTML to show and hide each View based on a URL parameter.

Add this custom shortcode to inspect URL parameters:

function exists_in_repeating_url_param_func($atts) {
  $var = $atts['var'];
  $test = $atts ['test'];
  $exists = isset($_GET[$var]) ? in_array( $test, $_GET[$var] ) : 0;
  return $exists;
}
add_shortcode("exists_in_repeating_url_param", "exists_in_repeating_url_param_func");

Use it like this:

[exists_in_repeating_url_param var="wpv-post-typer" test="video"]

If the URL includes "wpv-post-typer%5B%5D=video", this shortcode will return 1. If not, the shortcode will return null. You can use the value of this shortcode to determine whether or not to display each View.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by Ido Angel 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#617738

Hey,

Is there a way I can divide search results by category/taxonomy?
For example:

I have 4 categories: articles, video, audio, events.
I am searching for free text "basketball".
I want the results to show:


Results for "basketball" under "articles":
article-1, article-2, article 3

Results for "basketball" under "videos":
video-1, video-2, video-3

Results for "basketball" under "audio":
audio-1, audio-2, audio-3

Results for "basketball" under "events":
event-1, event-2, event-3

I tried creating a separate view for each category then placed all 4 views search results in one page. it worked, but if I now want to filter out just 1 category, I can't, since they are always in the same place.

Any ideas?

Cheers!

Ido

#618089

Hi, there's not a built-in way to group results like this this with Views. I assume your 4 Views are filtered by a taxonomy term, using a URL parameter, correct? You could try to use 4 different Views like you described, and combine that with conditional HTML that tests the URL parameter used for your taxonomy filter:

[wpv-conditional if="( '[wpv-search-term param='wpv-tax-param']' eq 'articles' )"]
Articles View here
[/wpv-conditional]
[wpv-conditional if="( '[wpv-search-term param='wpv-tax-param']' eq 'videos' )"]
Videos View here
[/wpv-conditional]

Replace 'wpv-tax-param' with whatever URL parameter you're using, and replace 'articles' and 'videos' with whatever the URL parameter value is when that term is selected in the filter.

#618238

thx C!

i made separate views and inserted them into a larger view, like you suggested. but the page doesn't load for hours, like there's a loop problem there somehow.

maybe i need to create a certain filter for each smaller view? like "parent view"?

thanks!
ido

#618450

I would not insert the 4 Views inside another View, I would insert them directly into a Post, Page, Content Template, or Layout Visual Editor cell. Can you take screenshots showing each View editor in wp-admin? I would like to see how you have the Query Filters configured. Then also take a screenshot showing how all 4 Views are placed on your site.

#618453

yes, i've tried that and it works - the only thing is i can't use ajax like that. i have to refresh the page.
and THEN i have to place submit buttons...

#618562

hey again,

ok - so forget ajax 🙂 still have a problem.

i want:

1. that the relevant view will appear initially, before there was any filter made
2. that i would be able to filter 2 types together

my code now is:

[wpv-conditional if="( '[wpv-search-term param='wpv-post-typer']' ne 'video' ) AND ( '[wpv-search-term param='wpv-post-typer']' ne 'video' )"]
{!{wpv-view name='article-searcher' view_display='layout'}!}
[/wpv-conditional]

[wpv-conditional if="( '[wpv-search-term param='wpv-post-typer']' ne 'articles' ) AND ( '[wpv-search-term param='wpv-post-typer']' ne 'audio' )"]
{!{wpv-view name='video-searcher' view_display='layout'}!}
[/wpv-conditional]

[wpv-conditional if="( '[wpv-search-term param='wpv-post-typer']' ne 'articles' ) AND ( '[wpv-search-term param='wpv-post-typer']' ne 'video' )"]
{!{wpv-view name='audio-searcher' view_display='layout'}!}
[/wpv-conditional]

i can achieve goal 1, but i can't achieve goal 2, because when i'm filtering 2 types togehter the url parameter changes from:

hidden link

to:

hidden link

and the condition fails.

#618714

Yes, the code I provided earlier was not intended to support multiple selections. This custom shortcode can test the presence of a value in a URL parameter with multiple values:

function exists_in_repeating_url_param_func($atts) {
  $var = $atts['var'];
  $test = $atts ['test'];
  $exists = isset($_GET[$var]) ? in_array( $test, $_GET[$var] ) : 0;
  return $exists;
}
add_shortcode("exists_in_repeating_url_param", "exists_in_repeating_url_param_func");

Use it like this:

[exists_in_repeating_url_param var="wpv-post-typer" test="video"]

If the URL includes "wpv-post-typer%5B%5D=video", this shortcode will return 1. If not, the shortcode will return null. You can use the value of this shortcode to determine whether or not to display each View.

Here's a document showing how to implement custom shortcodes in a conditional:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

#618727

perfect!
thx very much!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.