Skip Navigation

[Resolved] filter comparison: like vs contains

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)

This topic contains 0 replies, has 1 voice.

Last updated by Minesh 1 month ago.

Assisted by: Minesh.

Author
Posts
#2793306

I want to filter results on my page by the values of a custom field. The values are: easy, intermediate, advanced. However, my editors sometimes use mixed values and write something like: easy to intermediate, or intermediate/advanced.

I was hoping that I could set up the filter to 'using manually entered values', add the three main values, and use the 'like' comparison function, ensuring that when the user selects 'intermediate', the filter will find results marked as 'intermediate', but also as 'easy to intermediate' and 'intermediate/advanced'. I hoped it works like a 'contains' function which would pick all posts where the custom field contains the string 'intermediate'. This, however, doesn't work.

Is there anything I can do to make this work?

#2793445

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please send me problem URL and admin access details and let me check if there is any possible workaround I can share.

*** 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.

#2793688

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Lets talk about the filters you added. You added the Difficulty custom field filter and you allowed the user to select multiple options.

How does that works? As you already want to search in multiple options.

Lets say user selected option Easy and Advance - you does that should work? or Do you actually want to offer user to select one option and convert it to single select dropdown?

#2793807

Yes, I do want the user to be able to select multiple options in each filter. I don't think they'd be looking for 'easy' and 'advanced', but they will likely look for 'easy' and 'intermediate' or 'intermediate' and 'advanced'. If so, the filter should return all entries which contain at least one of the strings 'easy' or 'intermediate' in the Difficulty custom field.

#2793861

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now:hidden link

I've adjusted the code for the difficulty filter.

I've added the following view's filter hook to "Custom Code" section offered by Toolset with the code snippet "toolset-custom-code":
=> hidden link

add_filter( 'wpv_filter_query', 'func_filter_with_multiselect_multi_options', 10, 3);
function func_filter_with_multiselect_multi_options( $query_args, $view_settings, $view_id ) {
 
  if ( $view_id == 13603 ) { 
   
    
    if(isset($_GET['wpv-wpcf-difficulty']) and !empty($_GET['wpv-wpcf-difficulty']) ){
      
        $meta = $query_args['meta_query'];
     
        foreach($meta as $k=>$v):
            if($v['key'] == 'wpcf-difficulty'){
              		
              		$selected_options = $_GET['wpv-wpcf-difficulty'];
              		$difficulty_relation = array('relation'=>"OR"); 
              		foreach($selected_options as $p=>$q):
              			$difficulty_meta_args[] = array('key'=>'wpcf-difficulty',
                                           'value'=>$q,
                                           'type'=>'CHAR',
                                           'compare'=>'LIKE');
              
              		endforeach;
              		$difficulty_final_args = array_merge($difficulty_relation,$difficulty_meta_args);
                 
              	
              	unset($query_args['meta_query'][$k]);       
              	    
                $query_args['meta_query'][] =  $difficulty_final_args;
                             
                break;
              
                  
            }
        endforeach;  
      
    }
  
}

return $query_args;
}

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2794150

Thank you.
Will the same work for the Recommended Use filter? Should that be set up in the same snippet or a separate one?

#2794161

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now:hidden link

I've adjusted the code for the difficulty filter.

I've added the following view's filter hook to "Custom Code" section offered by Toolset with the code snippet "toolset-custom-code":
=> hidden link

add_filter( 'wpv_filter_query', 'func_filter_with_multiselect_multi_options', 10, 3);
function func_filter_with_multiselect_multi_options( $query_args, $view_settings, $view_id ) {
 
  if ( $view_id == 13603 ) { 
   
    
    if(isset($_GET['wpv-wpcf-difficulty']) and !empty($_GET['wpv-wpcf-difficulty']) ){
      
        $meta = $query_args['meta_query'];
     
        foreach($meta as $k=>$v):
            if($v['key'] == 'wpcf-difficulty'){
              		
              		$selected_options = $_GET['wpv-wpcf-difficulty'];
              		$difficulty_relation = array('relation'=>"OR"); 
              		foreach($selected_options as $p=>$q):
              			$difficulty_meta_args[] = array('key'=>'wpcf-difficulty',
                                           'value'=>$q,
                                           'type'=>'CHAR',
                                           'compare'=>'LIKE');
              
              		endforeach;
              		$difficulty_final_args = array_merge($difficulty_relation,$difficulty_meta_args);
                 
              	
              	unset($query_args['meta_query'][$k]);       
              	    
                $query_args['meta_query'][] =  $difficulty_final_args;
                             
                break;
              
                  
            }
        endforeach;  
      
    }
    
    if(isset($_GET['wpv-wpcf-use']) and !empty($_GET['wpv-wpcf-use']) ){
      
        $meta = $query_args['meta_query'];
     
        foreach($meta as $k=>$v):
            if($v['key'] == 'wpcf-use'){
              		
              		$selected_options = $_GET['wpv-wpcf-use'];
              		$recomonded_use_relation = array('relation'=>"OR"); 
              		foreach($selected_options as $p=>$q):
              			$recomonded_use_meta_args[] = array('key'=>'wpcf-use',
                                           'value'=>$q,
                                           'type'=>'CHAR',
                                           'compare'=>'LIKE');
              
              		endforeach;
             $recomonded_use_final_args = array_merge($recomonded_use_relation,$recomonded_use_meta_args);
                 
              	
              	unset($query_args['meta_query'][$k]);       
              	    
                $query_args['meta_query'][] =  $recomonded_use_final_args;
                             
                break;
              
                  
            }
        endforeach;  
      
    }
  
}

return $query_args;
}

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#2794202

Thanks! The filters work great in the View now, but in the Archive, the Recommended Use, Difficulty, and even the Medium and Year filters don't seem to do anything. I think they worked fine before.

#2794203

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As per our support policy - we entertain only one question per ticket. As all filter issues have been already addressed with this ticket. I suggest you should mark resolve this ticket and open a new ticket where you should share problem URL of your archive with brief description as well as admin access details.