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?
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?
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.
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.
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.
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?
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.
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?
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.
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: