Skip Navigation

[Resolved] define search filter custom range

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

Last updated by Tiit Sau 2 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#2249387
total-area front.png
total-area.png

Hi!

If custom field have a lot of values, pile of checkboxes looks ugly and isn't user friendly. How to define several ranges of custom field values and display as buttons?

Thanks in advance,
Tiit

#2249519

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

First of all, I would like to know what is the field type of custom field "Area" when you created it from Types Custom field group?

Basically, there is no option to display the fields as button but you can customize it on your own but first of all I would like to know how the "Area" field is configured from Custom Field group? Can you please share screenshot?

#2249533
total-area field.png
total-area grouped.png

Custom field 'Area' structure on the picture and how I wish the 'Area' filter looks like on the next one.
Code needs some correction of course.
[php]
[wpv-control-postmeta type="checkboxes" field="wpcf-total-area" source="custom" url_param="wpv-wpcf-total-area" values="10,15,20" display_values="max 10m², 10-20m²,20m² +"]
[php]
Does not have to be button. If works with one click , can be input or link.

#2249551

Minesh
Supporter

Languages: English (English )

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

But I would like to know first what is the original field type when you created the custom field "Area' from custom field group?

Can you please share screenshot of "Area" custom field group custom field group?

#2249567
total-area field in group.png

Field is created years ago as 'Single line'. Today I would set it as Number. No problem to change - it's development process.
Last question looks a little confusing. Nothing special there in field group.
In English it will be field group 'Product parameters', which appears on Post Type: Products.

#2249581

Minesh
Supporter

Languages: English (English )

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

Ok got it.

So, as you already find the way to display the field value in range, but you should add a range to values, for example:

[wpv-control-postmeta type="checkboxes" field="wpcf-total-area" source="custom" url_param="wpv-wpcf-total-area" values="0-10,11-15,>16" display_values="max 10m², 10-20m²,20m² +"]

You can adjust the range values as required for the "values" attribute in above shortcode.

Now, you will have to add view's filter "wpv_filter_query" to adjust the query for the custom field.

Please check the following related tickets:
- https://toolset.com/forums/topic/use-a-drop-down-menu-to-give-value-min-and-max/#post-1244052
- https://toolset.com/forums/topic/create-data-range-search-for-dropdown/

If you require further help, please let me.

#2249671
_my custom search.png

Can't get it work.
Check my function please:

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 == 32436 and isset($_GET['wpv-wpcf-total-area'])) {      
        $val = $_GET['wpv-wpcf-total-area'];
       
       $meta = $query_args['meta_query'];
        foreach($meta as $k=>$v):
            if($v['key'] == 'wpcf-total-area'){
                  
                    $filter_array = array(
                                array(
                                    'key'     => 'wpcf-total-area',
                                    'value'   => explode("-",$val),
                                    'type'    => 'numeric',
                                    'compare' => 'BETWEEN',
                                ),
                            );
                $query_args['meta_query'][] =  $filter_array;
                unset($query_args['meta_query'][$k]);                    
                break;
            }
        endforeach;
    
    }
          return $query_args;
 }
#2249689

Minesh
Supporter

Languages: English (English )

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

Can you please share problem URL and admin access details. And share where exactly you added the code you shared.

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

#2250571

Minesh
Supporter

Languages: English (English )

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

The first thing is that I was having impression that you are using the view and the "wpv_filter_query" will apply to only views not to WordPress archives. So, "wpv_filter_query" hook is not going to work in your case and we need to switch to "pre_get_posts" hook.

For WordPress archives, we will require to use the pre_get_posts or any other hook like posts_where etc..etc.. hook.

Now, I would like to know that are you working on the site currently as when I visit the archive page, I do not see the checkboxes are rendered:
=> hidden link

Is it due to the change you made where you change the field type from text to number?

Here is the reference ticket how you can hook in the meta_query using pre_get_posts hook, you can modify the hook code as required.
=> https://toolset.com/forums/topic/how-to-use-wpv_filter_query/#post-1206145

#2251255
total-area mix values.png

> I do not see the checkboxes are rendered

Because values were declared as ranges:

[wpv-control-postmeta type="checkboxes" field="wpcf-total-area" source="custom" url_param="wpv-wpcf-total-area" values="5-10,11-200" display_values="5-10m,11-200m"]

Remove values or just hyphens and checkboxes are there. For better tracking, I set the first value fixed and second as range.

[wpv-control-postmeta type="checkboxes" field="wpcf-total-area" values="10,11-200"

Now the first value appears, as expected and range does not. Check out again.

> you can hook in the meta_query using pre_get_posts hook, you can modify the hook code as required.

Minesh, can You adjust the code for my case please? It is difficult for me.

#2251451

Minesh
Supporter

Languages: English (English )

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

I've added the following filter hook with code to the functions.php file:

function func_archive_modify_query($query, $archive_settings, $archive_id){
    if($archive_id == 32436 ){//change this to the view ID of your archive page
	
	if(isset($_POST['search']['dps_general'])){
        foreach($_POST['search']['dps_general'] as $k=>$v):
            if($v['name'] == 'wpv-wpcf-total-area[]'){
				
				$range = explode("-",$v['value']);
				
				$meta_query[] = array(
				'relation'=>'AND',
				array(
                'key'     => 'wpcf-total-area', 
                'value'   => $range[0],
                'compare' => '>=',
				),
				array(
                'key'     => 'wpcf-total-area', 
                'value'   => $range[1],
                'compare' => '<=',
				)
				
				);
                 $query->set('meta_query',$meta_query);
            }
              
        endforeach;
		
		
   }
   
     }
}
add_action('wpv_action_apply_archive_query_settings','func_archive_modify_query',30,3);

Please check: hidden link

Note: For now I've set the archive's "Custom Search" setting to "Always show all values for inputs" so that you can see both checkboxes, you can tweak the setting once you confirm its working as expected.

#2252101
total-area filter settings.png
total-area yellow.png

Thanks Minesh,

But I haven't noticed any range in action. I painted "Total area" yellow, so we can see in the results what is actually available.
Look at filter settings on picture - are there choices "number> equal to" okay?

#2252123

Minesh
Supporter

Languages: English (English )

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

I see for one filter checkbox you set the value "5-8" and for another checkbox you set value "9".

You should set the range like you have set for first checkbox i.e. "5-8" and the current code is added according to range.

Is your filter checkboxes range is finalized? as we need to change the code accordingly. Can you please confirm the ranges you want to use?
or
is the current two checkboxes are finalized and that is how you want to use? If yes: we need to adjust the code accordingly.

#2252145

Okay, I got it.
Ranges for main view are set now as should be.
values="0-14,15-25,26-100" display_values="small,15-25m²,large"

#2252207

Minesh
Supporter

Languages: English (English )

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

We have to use the custom search setting "Always show all values for inputs" as we are using the custom range and custom range can be any so we can not determine what could be the available input filter options using the setting" Show only available options for each input".

So, I've change the filter setting to "Always show all values for inputs" and added the following code to the functions.php file:

add_action('pre_get_posts', 'registration_date_and_today',999,1);
function registration_date_and_today($query){
	
		
	
	
	if(is_archive('product') and isset($_REQUEST['search']['dps_general'])){
		

        foreach($_REQUEST['search']['dps_general'] as $k=>$v):
            if($v['name'] == 'wpv-wpcf-total-area[]'){
				
				$range = explode("-",$v['value']);
				
				$meta_query[] = array(
				'relation'=>'AND',
				array(
                'key'     => 'wpcf-total-area', 
                'value'   => $range[0],
                'compare' => '>=',
				),
				array(
                'key'     => 'wpcf-total-area', 
                'value'   => $range[1],
                'compare' => '<=',
				)
				
				);
                 $query->set('meta_query',$meta_query);
            }
              
        endforeach;
	}
}

Can you please confirm it works as expected: hidden link