Skip Navigation

[Resolved] Select Field Option to Show Product in All Results

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

Problem:

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 0
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

Solution:

You can try custom codes, for example:

https://toolset.com/forums/topic/select-field-option-to-show-product-in-all-results/#post-1645737

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 3 years, 10 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 5 replies, has 2 voices.

Last updated by nickP-12 3 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1642001

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

#1642825

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

#1644247
skill-level-filter.jpg
skill-level-code.JPG
skill-level.JPG

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

#1645737

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

#1649339

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.

#1649343

My issue is resolved now. Thank you!

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