Skip Navigation

[Resolved] WP_Query on the checkboxes value

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by Nigel 7 years ago.

Assisted by: Nigel.

Author
Posts
#503689
Cattura.PNG

hi all
i'm trying to use the checkboxes in WP_Query but i don't understand how.

i have this(image: cattura.png) checkboxes and in the database Types write this:

meta-key
wpcf-opzioni
meta-value
a:2:{s:64:"wpcf-fields-checkboxes-option-70d70a0ac8c2ea750ce8730b8f2c271c-1";a:1:{i:0;s:8:"visibile";}s:64:"wpcf-fields-checkboxes-option-124bfec15189252ee1755bf7329ef44c-1";a:1:{i:0;s:10:"importante";}}

how i can filter the posts with WP_Query?

$args_imp = array(	'post_type' => 'appuntamenti',
					'meta_query' => array(
						'key'     		=> 'wpcf-opzioni', 
						'value'   		=> HERE???????????????, 
						'compare' 	=> HERE???????????????,
					),
);
#503840

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Lorenzo

Complex fields such as checkboxes fields are stored as an array, and arrays are saved in the post_meta table as serialized strings (of the format you can see above).

This happens automatically when inserting data using update_post_meta, and is automatically reversed when extracting data using get_post_meta.

So if you use get_post_meta to get the value of the checkboxes custom field it will return an array, something like this:

Array
(
    [0] => Array
        (
            [wpcf-fields-checkboxes-option-827e103b3faa684adb1ab43f72d8bf7e-1] => Array
                (
                    [0] => visibile
                )
            [wpcf-fields-checkboxes-option-7f98f73cbfe89b28e0e31e74bf3c97f0-1] => Array
                (
                    [0] => importante
                )
        )
)

You can use array_values (hidden link) to convert that into a simple array where you don't need to know the exact names of the keys.

If you do something like this:

	$options = get_post_meta( get_the_ID(), 'wpcf-options' );
	$options_values = array_values( $options[0] );

then the resulting $options_values array will then look something like this, from which you can extract the value for your query:

Array
(
    [0] => Array
        (
            [0] => visibile
        )

    [1] => Array
        (
            [0] => importante
        )

)

Your meta query should be "=", but if you are going to be testing for multiple values (e.g. visibile and importante) then you will need to add multiple meta query terms with the relation 'AND' or 'OR' according to your needs.

You can see an example of such a query in the WP_Meta_Query class documentation (https://codex.wordpress.org/Class_Reference/WP_Meta_Query), or search for online tutorials if you are still unsure.

#505655

Thank you!
I have not had time to try! Tomorrow i try and tell you!
(I write this because the bot want close the thread! Asd)

#505981

ok... i try in the page and i can take every single elements of the array with

$options = get_post_meta( get_the_ID(), 'wpcf-options' );
$options_values = array_values( $options[0] );
for($i=0;$i<count($options_values);$i++){
	echo $options_values[$i][0];
}

but how i can write 'meta-query'? it is a double dimensional array so this don't work:

array(
	'key'     	=> 'wpcf-opzioni',
	'value'   	=> 'importante',
	'compare' 	=> 'IN', 
),
#506184

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Lorenzo

The meta query you write will be different depending on whether you have one or two terms to match (e.g. just 'importante' or both 'visibile' and 'importante').

You can use count (hidden link) to determine whether you are dealing with a single field or two fields, and you will have to construct the meta_query differently depending on the result.

If one you can output the value required using $options_values[0][0], if two you can output them using $options_values[0][0] and $options_values[0][1], but the meta_query will look slightly different as I described at the end of my last response.

The Views plugin is designed to overcome such difficulties if you are unfamiliar with PHP and WordPress meta queries by doing this for you without the need to write any code.

#506341

ok, Nigel...
but if i have a result with 2 fields how i can build the wp_query?

for example, i have this array in database(wpcf-opzioni):

Array
(
    [0] => Array
        (
            [wpcf-fields-checkboxes-option-827e103b3faa684adb1ab43f72d8bf7e-1] => Array
                (
                    [0] => visibile
                )
            [wpcf-fields-checkboxes-option-7f98f73cbfe89b28e0e31e74bf3c97f0-1] => Array
                (
                    [0] => importante
                )
        )
)

and i want querying the post that have "importante" field.
how i must write the query?

$args = array(	
	'post_type' => 'app',
	'meta_query' => array(	
		'relation' => 'AND', 
		array(
			'key'     	=> 'wpcf-opzioni', 
			'value'   	=> [???],
			'compare' 	=> [???], 
			'type'		=> [???], 
		),
		array(
			[...other code...] 
		),
	),
);

what i use instead of [???] to use a field in an array??
this is my problem... I hope I was clear! =/

thank you

#506361

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Lorenzo

Sorry, if you want to use Types to create custom queries then you need to have some basic PHP knowledge and know how to code such queries. We created Views precisely for users who want to build such queries without knowing how to code them.

The official documentation on meta queries is a little short on examples, but there is a nice example page here which demonstrates creating different kinds of meta queries which should be enough with the description above for you to be able to generate the meta query in the right format.

hidden link

#506377

we are not understanding....
does not matter, I will use the single checkbox!

#506595

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Lorenzo

Sorry, I do understand the issue, but writing custom meta queries is outside of our support policy, which is why I have been directing you to documentation showing how to do so.

Good luck with it, I hope you get it working.

The forum ‘Types Community Support’ is closed to new topics and replies.

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