Skip Navigation

[Resolved] Multiple custom field search with only one field

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.

Our next available supporter will start replying to tickets in about 0.22 hours from now. Thank you for your understanding.

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 7 replies, has 2 voices.

Last updated by fahimS-2 1 year, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2601367

I have 5 custom fields (cf1, cf2, cf3, cf4, cf5) which I have assigned to a custom post type. In the custom post archive I have added a search form and added cf1 as search field and kept the comparison as between. (I have taxonomy search field too in the form) Now I want to happen the search for cf2, cf3, cf4, cf5 with the same value for cf1. And I also want to keep OR logic between cf1, cf2, cf3, cf4, cf5

How can I do that?

#2601405

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please tell me how those custom fields are added using what field type?

Maybe you can share problem URL where you added the search form as well as admin access details and let me review your setting first and then I can tell you what could be the possible solution if any available.

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

#2601759

Minesh
Supporter

Languages: English (English )

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

I've added the following code to "Custom code" section with the code snippet "custom-field-search-within":
=> hidden link

function filter_scholarships( $query ){
    // only modify the scholarship archive
    if ( is_post_type_archive( 'dev' )  ) {
 
      $value = $_POST['search']['dps_general'][0]['value'];
    
      if(isset($query->query_vars['meta_query']) and !empty($query->query_vars['meta_query'])) {
      
        $meta_query =  $query->query_vars['meta_query']; 
     	$meta_query[] = array(
                'key'       => 'wpcf-cf2',
                'value'     => $value,
                'compare'   => '<='
            );
       $meta_query[] = array(
                'key'       => 'wpcf-cf3',
                'value'     => $value,
                'compare'   => '<='
            );
        $meta_query[] = array(
                'key'       => 'wpcf-cf4',
                'value'     => $value,
                'compare'   => '<='
            );
        $meta_query[] = array(
                'key'       => 'wpcf-cf5',
                'value'     => $value,
                'compare'   => '<='
            );
      
     $meta_query['relation']='OR';
    
     $query->set( 'meta_query', $meta_query );
        
     
        
    
    
    }
 
      //  
 
    }
}
add_action( 'pre_get_posts', 'filter_scholarships',999,1);

You are welcome to change the code as required.

#2601981

Hi,
Thanks for the code.

But unfortunately the code isn't performing the 'BETWEEN' comparison. No matter what input I provide in the search, It is showing all the posts.

I also want to apologize because I forgot to keep the the comparison 'between' in the search field 'cf1'. I changed it to 'BETWEEN' and again tried to search. But still what ever input I provide, the search is showing all the posts.

I tried to solve it by myself, but failed again and again. Can you kindly look into the problem again?

#2601997

Minesh
Supporter

Languages: English (English )

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

so you want to apply between? like you want to search between 5 and 15 for custom field 1 and the same you want to apply to other custom fields - correct?

#2601999

Yes, I want to keep the comparison as 'BETWEEN'. You can see in my first reply that I mentioned it 'kept the comparison as between'

But I again apologize because I somehow forgot to keep the comparison 'between' in the archive.

Thanks for your patience.

#2602049

Minesh
Supporter

Languages: English (English )

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

I've adjusted the code with "Custom Code" section with code snippet "custom-field-search-within":
- hidden link

function func_filter_range_within_custom_number_field( $query ){
    // only modify the scholarship archive
    if ( is_post_type_archive( 'dev' )  ) {
     
      $from_value  = $_POST['search']['dps_general'][0]['value'];
      $to_value  = $_POST['search']['dps_general'][1]['value'];
      $value = "$from_value,$to_value";
      if(isset($query->query_vars['meta_query']) and !empty($query->query_vars['meta_query'])) {
       
        $meta_query =  $query->query_vars['meta_query']; 
        $meta_query[] = array(
                'key'       => 'wpcf-cf2',
                'value'     => $value,
          		'type'		=> 'NUMERIC',
                'compare'   => 'BETWEEN'
            );
       $meta_query[] = array(
                'key'       => 'wpcf-cf3',
                'value'     => $value,
         		'type'		=> 'NUMERIC',
                'compare'   => 'BETWEEN'
            );
        $meta_query[] = array(
                'key'       => 'wpcf-cf4',
                'value'     => $value,
          		'type'		=> 'NUMERIC',
                'compare'   => 'BETWEEN'
            );
        $meta_query[] = array(
                'key'       => 'wpcf-cf5',
                'value'     => $value,
          		'type'		=> 'NUMERIC',
                'compare'   => 'BETWEEN'
            );
       
     $meta_query['relation']='OR';
              
     $query->set( 'meta_query', $meta_query );
             
    }
  
    }
}
add_action( 'pre_get_posts', 'func_filter_range_within_custom_number_field',999,1);

Can you please check it works as expected: hidden link

#2602079

My issue is resolved now. Thank you!