Skip Navigation

[Resolved] How to filter by more than one checkbox

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

Problem:

The metafield is not created by Types plugin but a standard worpress metafield, values are separated by comma
When searching by checkboxes, a single checkbox is checked, it return the good result, but more one is checked no results

[wpv-control-postmeta display_values="Famille,Noces/Anniversaire de mariage,Pension,Golf,Spa,Remise" values="Famille,Noces/Anniversaire de mariage,Pension,Golf,Spa,Remise" field="attributs_offres_intranet_wp" type="checkboxes" source="custom" url_param="wpv-attributs_offres_intranet_wp" output="legacy"]

Solution:

The checkboxes field in Views custom search form does not support the custom field you mentioned above:

but a standard worpress metafield
values are separated by comma

In you case, I suggest you try with some custom PHP codes, when user submit the search form, use filter hook "wpv_filter_query" to trigger a PHP function, in this function, get the "wpv-attributs_offres_intranet_wp" value submitted by user:

http://php.net/manual/en/reserved.variables.post.php

Apply them into the Views query one by one:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

See the example codes

https://toolset.com/forums/topic/how-to-filter-by-more-than-one-checkbox/#post-631805

Relevant Documentation:

This support ticket is created 5 years, 11 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 20 replies, has 2 voices.

Last updated by Luo Yang 5 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#631570

Hello
Just discovered also a new issue

When searching by checkboxes, Nigel suuggested to use the LIKE operator, this iw working if when 1 checkbox is checked, but if i check 2 checkboxes it is not working
(search is done on astrandard wp metafield where values are store as comma separated values

Thank you

#631764
checkboxes-filter.JPG

Hello,

Please elaborate this question with more details.

If it is a custom checkboxes field "My checkboxes" created with Types plugin, you can setup the custom field filter in your view like this:
Select items with field:
My checkboxes is a string equal to URL_PARAM(wpv-wpcf-my-checkboxes )

See screenshot: checkboxes-filter.JPG

#631766
results.jpg
noresults.jpg
filter1.jpg
metastore.jpg

Hello
The metafield is not created by Types plugin but a standard worpress metafield
values are separated by comma
When a sing le checkbox is checked, it return the good result but more one is checked no results

[wpv-control-postmeta display_values="Famille,Noces/Anniversaire de mariage,Pension,Golf,Spa,Remise" values="Famille,Noces/Anniversaire de mariage,Pension,Golf,Spa,Remise" field="attributs_offres_intranet_wp" type="checkboxes" source="custom" url_param="wpv-attributs_offres_intranet_wp" output="legacy"]

Thank you

#631775

Thanks for the details, The checkboxes field in Views custom search form does support the custom field you mentioned above:
but a standard worpress metafield
values are separated by comma

In you case, I suggest you try with some custom PHP codes, when user submit the search form, use filter hook "wpv_filter_query" to trigger a PHP function, in this function, get the "wpv-attributs_offres_intranet_wp" value submitted by user:
hidden link

Apply them into the Views query one by one:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

#631790

Hello

For wpv_filter_query, would this replace the entire query or can I specifically add the checkbox functionality and let everything else stay as it is?

Can you tell me if I'm in the good direction and help me to continue ?

add_filter( 'wpv_filter_query', 'wpv-attributs_offres_intranet_wp' );
function wpv-attributs_offres_intranet_wp( $query_args, $view_settings ) {
	
	
    
	if (isset($view_settings['view_id']) && $view_settings['view_id'] == 40885) {
        $query_args = array(
            'meta_key' => 'attributs_offres_intranet_wp',
			'meta_value' => '',
        );
    }
	

    return $query_args;
}

I have to get metavalue from url parameter ?

Thank you

#631792

Yes, you are using the right filter hooks, and I suggest you follow our document to setup your custom PHP codes:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

If you still need assistance for it, please provide a test site with same problem, also describe details for the problem. thanks

#631805

I have modified your PHP codes as below, please test again, check if it is fixed or not:


add_filter( 'wpv_filter_query', 'wpv_attributs_offres_intranet_wp', 99, 3 );
function wpv_attributs_offres_intranet_wp( $query_args, $view_settings, $view_id ) {
	if ($view_id == 40885 && isset($_GET['wpv-attributs_offres_intranet_wp'])) {
		
		$my_args = array('relation' => 'OR');
		foreach($_GET['wpv-attributs_offres_intranet_wp'] as $offre){
			$my_args[] = array(
				'key'     => 'attributs_offres_intranet_wp',
				'value'   => $offre,
				'compare' => 'LIKE',
			);
		}
		$query_args['meta_query'][] = $my_args;
		foreach($query_args['meta_query'] as $k=>$v){
			if(isset($v['key']) && $v['key'] == 'attributs_offres_intranet_wp'){
				unset($query_args['meta_query'][$k]);
			}
		}
	}
	return $query_args;
}
#631807
anatara.jpg

Hello
Thank you for your effort

If I check :

Famille + Golf + Spa

The search returns : Anantara Rasananda wich have 1 offer with only "Spa" as attribut

#631810

I assume you are going to setup as "AND" logic, you can replace this line from:
$my_args = array('relation' => 'OR');
To:
$my_args = array('relation' => 'AND');

And it is only an example, you will need to customize the PHP codes to what you want.

#631813

Very good

Just a last question :
Famille + Golf + Spa CHECKED

it returns : Casadelmar
wich have 2 offers attached to it :

First offer has : Famille / Golf
second offer : Golf / Spa

NORMALLY, this offer will not be returned

Is this difficult to Fix or not ?

Thank you in advance

#632179

I am not sure how do you setup the custom field "attributs_offres_intranet_wp" and the filed values, so I am not sure if it is possible to fix, since it is a custom PHP codes problem, please provide a database dump file (ZIP file) of your website in below private detail box, I need to test and debug it in my localhost, thanks

#633570

Thanks for the details, I am downloading the files, will update this thread if there is anything found

#638101

Here are what I found:
1) I can import your database into my localhost, but the post type "Offres" isn't created with Types plugin, I so I can not edit the view "Recherche Offres intra SOS" in my localhost,, I still need to test it in your website.

2) I have done below modification in your website:
a) Edit the view "Recherche Offres intra SOS", in section "Query Filter", add below filter:

attributs_offres_intranet_wp is a string equal to URL_PARAM(wpv-attributs_offres_intranet_wp)

This filter is required in your case, or the filter won't work as expected.

b) For debug, in section "Élément de boucle dans Recherche Offres intra SOS", add a line:

[wpv-post-field name="attributs_offres_intranet_wp"]

It will be able to output the field value of "attributs_offres_intranet_wp":
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-field

Test it in front-end:
hidden link

enable options Famille + Golf + Spa, and search, in the result:

I can see both items have field value Famille + Golf + Spa
Which is different from you mentioned above:

First offer has : Famille / Golf
second offer : Golf / Spa

Can you confirm it? There isn't the same problem as you mentioned above:

NORMALLY, this offer will not be returned

#640292
offer1.jpg
offre2_1.jpg
offre2_2.jpg

Hello
Thank you for your reply

(you can login with same credentials as before)
Test url :
hidden link

I try to explain better :

Please see here :
Product1 : Anantara Rasananda has 1 offer with "Famille" and "Golf" checked
Product2 : Casadelmar – Corse has 2 offers : offer1 with "Famille" and "Golf" checked and offer2 with "Golf" and "Spa"

In search criteria, if we check : "Famille" and "Spa" and "Golf" normally no product is returned !

Thank you

#640992
bouchairY.JPG

The URL you mentioned above is empty, I can not see the screenshot you mentioend above, and it does not help anything.

Please click below URL:
hidden link

See screenshot:
the 1st post "Amanzoe, Péloponnèse – Grèce", field "attributs_offres_intranet_wp" value is: Famille,Golf,Spa
the 2nd post "Casadelmar – Corse", field "attributs_offres_intranet_wp" value is: Famille,Golf, Golf,Spa

Both posts have values in custom field "attributs_offres_intranet_wp": Famille+ Golf + Spa

In your screenshot:
hidden link

value "Famille/Golf/" is separated by "/", which is different from the original question of this thread:
https://toolset.com/forums/topic/how-to-filter-by-more-than-one-checkbox/#post-631570
values are store as comma separated values

So they might be two different custom fields, I suggest you check it, make sure you are using custom field "attributs_offres_intranet_wp" to store the values, see your own post:
https://toolset.com/forums/topic/how-to-filter-by-more-than-one-checkbox/#post-631790

If you still need assistance for it, please describe detail steps for how can I see the screenshot you mentioned above:
hidden link

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.