Home › Toolset Professional Support › [Resolved] Filter view with the same URL parameter
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
Tagged: Views, Views plugin
This topic contains 11 replies, has 2 voices.
Last updated by Puntorosso 3 years, 10 months ago.
Assisted by: Minesh.
Hi, trying to understand the OR logic of a view's filter.
I have a view with 2 custom fields filters, using URL parameters
color -> string = URL Parameter "color"
OR
size -> string=URL Parameter "size"
and these searches are working:
hidden link
hidden link
But if I want to use the same URL parameter in the filter, like
color -> string = URL Parameter "src"
OR
size -> string=URL Parameter "src"
I get 0 returns when using these searches:
hidden link
hidden link
Is this a wanted behavior?
Do I miss something?
Hello. Thank you for contacting the Toolset support.
As I understand, you must have created view with filters and you want to assign the custom URL param for your search filters.
You said:
I get 0 returns when using these searches:
- hidden link
- hidden link
==>
Its not logical to assign the same URL param for color and size (as you done src) as both filters are having different values. The URL params must be unique.
So, if you add a filter with:
- hidden link
- hidden link
===>
Notice that src1 and src2 are different URL params and you will have to adjust the URL prams with the Query Filter section of your view.
Hi, I know that with 2 different URL parameters work (that's what I use normally), but my question was exactly on why it doesn't work, when using the same URL parameter for all filters.
Let's say I am selling t-shirts and I set these search filters:
color -> string = URL Parameter "src" OR size -> string=URL Parameter "src"
The URL
<em><u>hidden link</u></em>
should deliver all t-shirts which color="M" OR size = "M"
It probably won't find any t-shirt with a "M" color, but it should output all t-shirts size "M", as we are using the OR operator.
Why I get zero results instead? This doesn't seem logical.
I just run a test and with classic view we have option to modify the URL param but not with block view.
I would like to have bit more information.
How do you setup your view, using blocks or classic views?
Can I have problem URL where I can see the issue and admin access details? Let me see what query is generated.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
I just checked your view's settings and I found that we have a known issue when you set OR clause between multiple custom fields or taxonomy as well as we have another issue with checkboxes field type when you pass the URL param from URL.
However, I've workaround to share for that, I've added the following view's hook to "Custom Code" section offered by Toolset:
=> hidden link
add_filter( 'wpv_filter_query', 'func_adjust_filter_relation', 1000 , 3 ); function func_adjust_filter_relation( $query_args, $view_settings ) { if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] ==2688) ) { $query_args['meta_query']['relation'] = 'OR'; $target_field = "wpcf-obj-view"; // change this field slug to your field slug if(isset($_GET['src']) and !empty($_GET['src']) ) { foreach ($query_args['meta_query'] as $k => $v): if (isset($v['key']) and $v['key'] == $target_field){ $query_args['meta_query'][$k]['value'] = $_GET['src']; } endforeach; } } return $query_args; }
Can you please confirm it works as expected?
Not exactly.
This solves only the problem with the "wpcf-obj-view" field, but not with other fields included in the View (like "obj-rent-licence")
This link should deliver 15 items which have an available rent license:
hidden link
But I get zero results.
I've to adjust the code I've added to "Custom Code" section as given under:
add_filter( 'wpv_filter_query', 'func_adjust_filter_relation', 1000 , 3 ); function func_adjust_filter_relation( $query_args, $view_settings ) { if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] ==2688) ) { $query_args['meta_query']['relation'] = 'OR'; $target_field = "wpcf-obj-view"; // change this field slug to your field slug $found = 0; if(isset($_GET['src']) and !empty($_GET['src']) ) { foreach ($query_args['meta_query'] as $k => $v): if (isset($v['key']) and $v['key'] == $target_field){ $query_args['meta_query'][$k]['value'] = $_GET['src']; $found = 1; } endforeach; if(!$found){ unset($query_args['post__in']); $query_args['meta_query'][] = array('key'=>'wpcf-obj-view', 'compare'=>"LIKE", 'value'=>$_GET['src'], 'type'=>'char'); } } } return $query_args; }
I can see 15 results now with the following URL: hidden link
Thanks for your help, but still doesn't work properly.
I have added now another OR custom field "Pool" and I get again zero results.
The problem is that is not really viable to edit the custom code every time we add a filter item.
IMHO the complete operator logic of the Views filter should be revisited.
Every day the expectations of customers are getting more and more complex, so we should expect more Views filters flexibility.
Thanks for your help, but still doesn't work properly.
I have added now another OR custom field "Pool" and I get again zero results.
==>
The thing is that with checkboxes custom field we have a known filtering issue when you try to filter the view result for the checkboxes custom field using URL param that is added directly from URL, in your case src=somevalue.
The known issue is files since long and our devs yet wont able to reach there or there is no fix available that is why I try to share the workaround with you. You will require to tell me what fields you want to filter with so that I can share working workaround with you.
Thanks for your help Minesh.
In this particular case, the customer add/remove search parameters regularly.
The best way would be if the code would include an array of custom fields, which is easier to change/maintain.
Yes - we can add array of custom fields but you will require to tell me what fields you want to include.
However, As far as I know, the issue will only happen when you try to add the field whose field type is checkboxes. Other fields should work OK and until now, we only have issue with checkboxes field type.
So, if you agree, you can send me array of checkboxes field type you want to include and I will try to adjust the code accordingly.
I really appreciate your workaround, but we have decided to use the classic query, with different URL parameters.
Thanks a lot for your help.