Skip Navigation

[Resolved] Use a drop down menu to give value min and max

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

Problem:
Use a drop down menu to give value min and max

Solution:
You should try to use the view's filter hook "wpv_filter_query" to modify the view's query on fly.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/use-a-drop-down-menu-to-give-value-min-and-max/#post-1244052

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

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

Last updated by seanM-7 5 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1243328

In a Search in a View, I'd like to put in a drop down menu that gives ranges of numbers, such as 1-10, 11-50, 51-100. Then the search filter looks for values in a numeric field that are in that range. Is this possible? Thank you!

#1243358

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - you should try to add your search filter for your custom field with custom values.

For example:

<div class="form-group">
    <label>[wpml-string context="wpv-views"]Number of pages[/wpml-string]</label>
    [wpv-control-postmeta field="wpcf-FIELD-NAME" type="select" source="custom" values="1-10,11-50,51-100" display_values="1-10,11-50,51-100" url_param="wpv-wpcf-FIELD-NAME"]
</div>

Where:
- Replace "FIELD-NAME" with your original field name.

Add the following code to your current theme's functions.php file:

add_filter('wpv_filter_query', 'func_range_search_custom_field', 10, 3);
function func_range_search_custom_field($query, $views_setting,$view_id) {
    if($view_id == '614' && isset($_GET['wpv-wpcf-FIELD-NAME'])) {

      $val = $_GET['wpv-wpcf-FIELD-NAME'];
       
       $query['meta_query']= array(
                                array(
                                    'key'     => 'wpcf-FIELD-NAME',
                                    'value'   => explode("-",$val),
                                    'type'    => 'numeric',
                                    'compare' => 'BETWEEN',
                                ),
                            );
    }
          return $query;
 }

- Replace "FIELD-NAME" with your original field name.

#1243632

Thanks Minesh. It's not quite there yet. For some odd reason, the drop down <select> menu is not populating with <option> tags.

Right now, I've got the dropdown menu code as:

<label>[wpml-string context="wpv-views"]Employees[/wpml-string]</label>

[wpv-control-postmeta field="wpcf-client-employee" type="select" default_label="Any" source="custom" url_param="wpv-wpcf-client-employee" values="1-100000,1-10,11-25" display_values="Any,1-10,11-25"]

You can see this at our development site at hidden link (It's the EMPLOYEES dropdown)

The code for the function appears to be working OK so far. Any thoughts on why the dropdown would refuse to work?

#1243639

Minesh
Supporter

Languages: English (English )

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

I need to check on your install, whats going wrong.

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

#1243710

Minesh
Supporter

Languages: English (English )

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

Well - I've set the custom search settings as you can see with the following screenshot to display "Always show all values for inputs":
=> hidden link

I can see now the field is loaded with all options.

#1243727

I figured it was something simple like that. Thank you so much, it looks like it's doing exactly what we're looking for now.

#1243729

My issue is resolved now. Thank you!

#1243733

Oops, I may have spoke too soon. It looks like the other filters (Industry and State) are not working now. The dropdowns work, they just aren't filtering. Should I add them to the meta_query array in the functions.php?

#1243903

Minesh
Supporter

Languages: English (English )

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

Well - As the ticket was resolved, the private information you shared was deleted automatically.

Can you please share access details again.

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

#1244052

Minesh
Supporter

Languages: English (English )

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

I've adjusted the code as given under:

add_filter('wpv_filter_query', 'func_range_search_custom_field', 10, 3);
function func_range_search_custom_field($query_args, $views_setting,$view_id) {
    if($view_id == 3750 and isset($_GET['wpv-wpcf-client-employee'])) {
 
	
		$val = $_GET['wpv-wpcf-client-employee'];
	  
	   $meta = $query_args['meta_query'];
        foreach($meta as $k=>$v):
            if($v['key'] == 'wpcf-client-employee'){
                 
                    $filter_array = array(
                                array(
                                    'key'     => 'wpcf-client-employee',
                                    'value'   => explode("-",$val),
                                    'type'    => 'numeric',
                                    'compare' => 'BETWEEN',
                                ),
                            );
				$query_args['meta_query'][] =  $filter_array;
                unset($query_args['meta_query'][$k]);                    
                break;
            }
        endforeach;
		
	 
       
    
	}
          return $query_args;
 }

Can you please confirm now filter works as expected:
=> hidden link

#1244100

My issue is resolved now. Thank you! I've been testing your solution for a while and everything it working as it should. Thank you so much. That's a solution I'll probably be re-using in the future. Thanks again!