Skip Navigation

[Resolved] Pre-selection filter based on URL condition in View

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

Problem:

Setup default value of shortcode [wpv-control-post-taxonomy]

Solution:

The shortcode [wpv-control-post-taxonomy] does not support setup default value by shortcode attribute.

But it supports setup default value by URL parameter, in your case, you can try to pass URL parameter "wpv-lang" to the page, for example:
https://mysite.com/page-url/?wpv-lang=en

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-control-post-taxonomy

This support ticket is created 4 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 3 voices.

Last updated by columD 4 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1834751
317a3de7c2132e09ef6b1d4c616cdf4e.png

My site is auto-translated and I only want certain pages to show up for a certain country.

For example:
When someone enters the website hidden link (English)
If URL doesn't contain /fr/ then EN checkbox is precheck.
EN - X

When someone enters the website hidden link (French)
If URL contain /fr/ then FR checkbox is precheck.
FR - X

If you have an alternative solution that might work with what I need, let me know.

#1835341

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You have a View that includes a filter for the checkboxes field with options for the language as shown in the screenshot, yes?

And you want that View to be filtered based on the language set in the URL?

You can add a Query Filter to the View for the checkboxes field and have its value come from a URL, but a URL parameter, so if the URL was mysite.com/?lang=fr then you could set the filter by the URL parameter "lang".

The same isn't directly possible with the URL format that you are currently using.

In that case you would need to provide the value for the filter by a shortcode attribute instead. You would need to use the legacy editor for Views to create the View and then insert it with the wpv-view shortcode, and add a custom attribute used by the filter, the value for which comes from a custom shortcode (you would need to write) that extracts the language code from the URL and returns just that part.

So if you registered a custom shortcode "language" that did that, you would insert your View like so

[wpv-view name="my-view" lang="[language]"]

and your Query Filter would be set up to get its value from the "lang" shortcode attribute.

You can read about creating custom shortcodes here: https://developer.wordpress.org/plugins/shortcodes/

#1835635

Hi Nigel,

I do want a solution that works with the current view. When the view filter is displaying all results. Is it possible to use JS to pre-select the option based on the URL on the first load and every time the AJAX refreshes?

Or I am thinking of using If-so plugin to see if that works. is there a way to preselect taxonomy with toolset view filter shortcode?

[wpv-control-post-taxonomy value='en' taxonomy='lang' url_param='wpv-lang' type='select']

I can maybe use If-so to determine which filter shortcode or language to execute.

#1836169

Hello,

The shortcode [wpv-control-post-taxonomy] does not support setup default value by shortcode attribute, see our document:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-control-post-taxonomy

But it supports setup default value by URL parameter, in your case, you can try to pass URL parameter "wpv-lang" to the page, for example:
hidden link

#1836609

Hi Luo,

Thanks for helping. That wouldn't work because I want pre-selected language when people come in the site.

Can you take a look at this code to see if it's sufficient?

jQuery(document).ready(function($) {
      if (window.location.href.indexOf("/fr/") > -1) {
               jQuery("select[name^='wpv-lang'] option[value='fr']").attr("selected","selected").trigger('change');;
   }
   else if (window.location.href.indexOf("/de/") > -1) {
         jQuery("select[name^='wpv-lang'] option[value='de']").attr("selected","selected").trigger('change');;
  }
  else {
               jQuery("select[name^='wpv-lang'] option[value='en']").attr("selected","selected").trigger('change');;
  }
});
#1838323

I think your custom JS codes is good, for the question: if it's sufficient?
That depends on yourself, if it can achieve your request, then it is sufficient

#1839139

My issue is resolved now. Thank you!