CustomPress fields & custom loop; how to filter on one checkbox option?

I'd like to create custom loops that filter posts based on checkbox options set up with CustomPress Fields.

I notice that these get stored as serialised arrays, but I don't know how I should unserialise that data for comparing in the query arguments.

The one I've done so far is easy, as it's a single "Yes" checkbox which it either there or not, so the following works:

$args = array(
	'meta_key'   => 'ct_Show_on_Na_checkbox_d9ff',
	'meta_value' => 'a:1:{i:0;s:3:"Yes";}',
	'posts_per_page'  => 20
);

But next I need to create checkbox fields which will have multiple options, for example...

Fruit: [_] Apple [_] Orange [_] Banana

...and be able to query with something like:

$args = array(
	'meta_key'   => 'ct_Fruit_checkbox_d9ff',
	'meta_value' => 'Banana',
	'posts_per_page'  => 20
);

Does anyone know if this is possible and what the PHP would be?

Thanks in advance :slight_smile:

  • Rainbow Web Services
    • Site Builder, Child of Zeus

    Awwww, thanks, Michael. No cigar though, I'm afraid.

    This works:

    $args = array(
    	'meta_key'   => 'ct_Show_on_Na_checkbox_d9ff',
    	'meta_value' => 'a:1:{i:0;s:3:"Yes";}'
    );

    But this doesn't:

    $args = array(
    	'meta_query' => array(
    		'key'     => 'ct_Show_on_Na_checkbox_d9ff',
    		'value'   => 'Yes',
    		'compare' => '='
    	)
    );

    And neither does this:

    $args = array(
    	'meta_query' => array(
    		'key'     => 'ct_Show_on_Na_checkbox_d9ff',
    		'value'   => 'Yes',
    		'compare' => 'LIKE'
    	)
    );
  • Ash
    • WordPress Hacker

    Hello @Mina

    I hope you are well today.

    Would you please try this:

    $args = array(
    	'meta_key'   => 'ct_Show_on_Na_checkbox_d9ff',
    	'meta_value' => maybe_serialize( array( "Yes" ) )
    );

    If it works, then for multiple value, then try this:

    $args = array(
    	'meta_key'   => 'ct_Show_on_Na_checkbox_d9ff',
    	'meta_value' => maybe_serialize( array( "value 1", "value 2", "value 3" ) )
    );

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Rainbow Web Services
    • Site Builder, Child of Zeus

    That doesn't seem to work, Ashok, sorry. I tried:

    $args = array(
    	'meta_query' => array(
    			'key'     => 'ct_Show_on_Na_checkbox_d9ff',
    			'value'   => maybe_serialize( array( "Yes" ) ),
    			'compare' => 'LIKE' // tried with *and* without this line
    		),
    	'posts_per_page'  => 20 // also tried with this line removed, and it defaulted to 10 per page as expected
    );

    With all of these variations, I just get the full set of results delivered. meta_query seems to have no effect at all. I even tried putting in an incorrect value in key and value, and the query just returns the full set of results. Surely if the meta_query argument was working at all, it would return no results if I specify a field name that doesn't exist?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.