Skip Navigation

[Resolved] Filter results automatically with URL string

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

Problem: I have two custom Views set up, one on a Page and the other in the template for a custom post type. The View on the custom Page includes links to the individual custom posts. If the User filters the View on the custom Page using one specific taxonomy filter, I would like to carry that filter selection over into the single post View so that the View is filtered using the same taxonomy term.

Solution: Add the URL parameter from the Page search to the links to each individual post in the View results. Use the wpv-search-term shortcode in conditional blocks in the View results on the custom Page to create the appropriate links to the individual posts with URL parameters applied as expected. See the examples here for more details. Example conditional criteria:

( ( '[wpv-search-term param="wpv-style-technique" ]' eq 'paintings' ) ) 

Relevant Documentation:
https://toolset.com/course-lesson/using-toolset-conditional-block/
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-search-term
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-url

This support ticket is created 2 years, 11 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.

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 8 replies, has 2 voices.

Last updated by Marcel 2 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#2034245

This site contains profiles for artists. On one page, visitors can filter the results on a custom field "techniques", with which they can filter what kind of artists they want to see, like painters, sculpturists or photographers.

The problem here is that one artist often uses multiple techniques. When a visitors filters the artists on "painters", a certain painter can also be a photographer. However, the visitor that filters on painters is probably not interest in photography.

On the artist single page, all the artist's works are shown (all techniques). They can be filtered by technique there as well, but we would like to do that automatically if there's a filter selected on the first page.

I guess that's possible since the filter value is visible in the URL, but I can't figure out how to do this...

#2034527
Screen Shot 2021-04-27 at 2.25.29 PM.png

Hello, if I understand correctly, you want to adjust the links in the search results of the first page so that they include a URL parameter with a specific value when the User searches by the technique field. The value of that parameter should match whatever value the User selects in the technique field. You can set this up with some of our custom shortcodes, and conditional blocks in the results of your View or archive. We offer a shortcode that can be used in a View to check for the presence of any URL parameter - wpv-search-term:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-search-term
Another shortcode to access the post URL, which we will use to create a custom link tag:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-url

On your Artists search page, if you select the Paintings technique, the URL parameter wpv-style-technique appears with the value paintings:
wpv-style-technique=paintings

It looks like the search on the single Artist post works with a slightly different URL parameter - wpv-style-techniques:
hidden link

So in the search results of the first search page, you could set up conditionals that test the value of that URL parameter using the wpv-search-term shortcode. Add a conditional block to the result loop, and use the advanced conditional editor feature to set this conditional criteria:

  ( ( '[wpv-search-term param="wpv-style-technique" ]' eq 'paintings' ) ) 

See the screenshot attached here showing the advanced editor.

Then place a custom HTML block so it is nested inside the conditional block. This content will be shown when the conditional is true, meaning the URL parameter exists and equals 'paintings'. In this custom HTML block, you can create a custom link with the single Artist post URL parameter 'wpv-style-techniques' applied as desired for the relevant paintings technique, like so:

<a href="[wpv-post-url]?wpv-style-techniques=paintings">[wpv-post-title]</a>

Now when the User clicks the link in the results after selecting the paintings technique, the link URL will automatically include the correct URL parameter to lead the User to the single Artist page with filter already applied.

You'll need to create similar conditional blocks for each possible URL parameter, and another conditional block where the URL parameter equals a blank string to handle the case where no filter value is selected. The conditional criteria for that case will look like this:

  ( ( '[wpv-search-term param="wpv-style-techniques" ]' eq '' ) ) 

In each conditional block, you can nest the contents you want to include when the URL parameter exists and is equal to some specific value, or when it does not exist. Let me know if you have questions about this approach and I can offer more direct guidance.

#2034563

Hi Christian,

Thanks for your quick and extensive reply. Okay, so this is gonna take a bit more time and effort than I hoped... I'll have to get back to you on that. Thanks again for now.

Best regards,

Marcel

#2034565

Of course, I understand and I will stand by for your update.

#2041921

Just a reply to keep the topic open 😉

#2042423

Understood, thanks.

#2044979

Hi Christian,

Well, that was actually easier then I expected 🙂

I got it to work, but encounter 1 (small?) problem. I also need a conditional output when there's no filter selected. I got that to work, but that shouldn't be shown if one of the techniques is selected. I can't get that to work...

So I've tried 2 conditions:

( ( '[wpv-search-term param="wpv-style-technique" ]' ne 'paintings' ) AND ( '[wpv-search-term ]' ne 'photography' ) AND ( '[wpv-search-term ]' ne 'prints' ) AND ( '[wpv-search-term ]' ne 'sculptures-objects-installations' ) AND ( '[wpv-search-term ]' ne 'works-on-paper' ) )

That works for the paintings filter (the first one) but not for the rest. With another technique selected, both the output with the filter is shown, and the output without the filter. Setting it to OR doesn't work at all.

I also tried ( empty( '[wpv-search-term ]') ) , that doesn't work at all either.

#2045011

I got it to work, but encounter 1 (small?) problem. I also need a conditional output when there's no filter selected. I got that to work, but that shouldn't be shown if one of the techniques is selected. I can't get that to work...
Check the last example in my previous comment, showing how to set up a conditional that tests for the case where no Technique filter is selected: https://toolset.com/forums/topic/filter-results-automatically-with-url-string/#post-2034527
When set up correctly, the contents inside this conditional will not be displayed when any Technique filter is selected. So you will have one conditional block for each possible option in the Technique filter. I noticed a small typo in the code I shared before, I left off the "s" in techniques. I have corrected that now in the code example.

One possible reason your code was not working as expected is that param="wpv-style-techniques" is missing from the wpv-search-term shortcodes for all the criteria except 'paintings'. Compare these wpv-search-term shortcodes in your conditional criteria. This one is correct:

 ( '[wpv-search-term param="wpv-style-techniques" ]' ne 'paintings' ) 

...these are incorrect:

 ( '[wpv-search-term ]' ne 'photography' ) 
 ( '[wpv-search-term ]' ne 'prints' )
 ( '[wpv-search-term ]' ne 'sculptures-objects-installations' )
 ( '[wpv-search-term ]' ne 'works-on-paper' )

The param attribute and "wpv-style-techniques" value is required in each case, because it specifies which URL parameter we are testing in this conditional.

If I understand your setup correctly, however, this is an inefficient method for setting up a conditional that tests for the case where no filter is selected. See the last example in my previous comment for a better method using one conditional clause instead of multiple conditional clauses joined by "AND":

( ( '[wpv-search-term param="wpv-style-techniques" ]' eq '' ) ) 
#2045023

Yes, that was the problem. Thanks Christian 🙂

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