Tell us what you are trying to do?
Hi, our site is offering courses and I have created a custom field to assign a difficulty using a select field. The options are:
All Abilities
Easy - Value 1
Medium - Value 2
Hard - Value 3
In my view for the courses I have a filter using the same field so that clients can filter the courses based on their ability. My problem is how do I make the courses defined as "All Abilities" appear regardless of what is selected (i.e. give it a value of 1, 2 and 3) - or another workaround to achieve the same result?
Thanks,
Nick
Hello,
There isn't such kind of built-in feature within Toolset plugins, you can try custom codes, for example, add below codes into your theme file "functions.php":
add_filter('wpv_filter_query', function($query, $settings, $view_id){
if($view_id == 123){
if(isset($query['meta_query'])){
foreach($query['meta_query'] as $k=>$v){
if($v['key'] == 'wpcf-difficulty'){
unset($query['meta_query'][$k]);
$query['meta_query'][] = array(
'relation' => 'OR',
$v,
array(
'key' => 'wpcf-difficulty',
'compare' => 'NOT EXISTS',
),
);
break;
}
}
}
}
return $query;
}, 10, 3);
Please replace 123 with your view's ID, replace "difficulty" with your custom field "difficulty" slug
More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Hi Luo,
Thanks for your assistance I've tried the code and made the edits however there doesn't seem to be any affect with courses categorised as "All Abilities" not showing when Begginner, Intermediate or Advanced is selected - my PHP is limited hence the requirement for ToolSet.
I've added a couple of screenshots incase they add clarity, however courses categorised as "All Abilities" are not showing when Begginner, Intermediate or Advanced is selected.
Thanks again,
Nick
Thanks for the details:
In your screenshot:
https://toolset.com/wp-content/uploads/2020/05/1644247-skill_level.jpg
The option "...All Abilities" is using value "0".
Please try to replace the PHP codes as below:
add_filter('wpv_filter_query', function($query, $settings, $view_id){
if($view_id == 974){
if(isset($query['meta_query'])){
foreach($query['meta_query'] as $k=>$v){
if( isset($v['key']) && $v['key'] == 'wpcf-skill-level' ){
$query['meta_query'][$k]['compare'] = 'IN';
$query['meta_query'][$k]['value'] = array(0, $v['value']);
break;
}
}
}
}
return $query;
}, 10, 3);
And test again
Hi,
Yes, sorry, I had to set the value to 0 otherwise (being a required field) it would not let the user continue if selecting "All Abilities" when it didn't have a value.
I've tried the new code snippet and it appears to be working! Thank you so much for your asssistance.
All the best.
My issue is resolved now. Thank you!