WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to query post meta fields, filtered by a multi checkbox


Currently, the query works only with second parameter (People) when the form is submitted.

How can I filter with two parameters?

HTML (Form submit)

<input type="checkbox" id="inlineCheckbox1"  name="category[]" value="Planet">
<input type="checkbox" id="inlineCheckbox2" name="category[]" value="People">

PHP (Search)

                $category = $_GET['category'];

                $category_planet = $category[0];
                $category_people = $category[1];

                $argsearch      = array();                    
                $argsearch["post_type"] = "projects";
                $argsearch['showposts'] = -1;

                if( $advtext!="" ){
                    $advtxtQuery ="select ID from $wpdb->posts
                    LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id where {$wpdb->posts}.post_status = 'publish'  ";
                    $where =" (( post_typs LIKE 'projects') AND ( post_title LIKE '%".$advtext."%')) ";
                    $where .=" AND ( $wpdb->postmeta.meta_key = '_cmb_project_category' AND $wpdb->postmeta.meta_value LIKE '%".$advtext."%')";
                    $mypostids = $wpdb->get_col($advtxtQuery." AND ( ".$where." )" );

                        $argsearch["s"] = $advtext;


                if( $category_planet!="" ){
                        $argsearch["meta_query"] = array();
                    $argsearch["meta_query"][] = array(
                            'key' => '_cmb_project_category',
                            'value' => $category_planet,
                            'compare' => 'LIKE'


                if( $category_people!="" ){
                          $argsearch["meta_query"] = array();
                     $argsearch["meta_query"][] = array(
                               'key' => '_cmb_project_category',
                               'value' => $category_people,
                               'compare' => 'LIKE'


                global $wp_query, $post,$wpdb;
                $query =  new WP_Query($argsearch);
                $project_list = $query->post->ID;
share|improve this question
Thank you @toscho for help to improve my question I'm not good in English. Thank you very much. +1 – iDev Man Nov 4 '12 at 16:03
up vote 1 down vote accepted

You have a typo in your $advtxtQuery query-- $where =" (( post_typs LIKE 'projects')...

For the rest, both queries are searching on the same key so use IN instead of trying to create two separate arrays, as in the Codex example below:

 $args = array(
   'post_type' => 'my_custom_post_type',
   'meta_key' => 'age',
   'orderby' => 'meta_value_num',
   'order' => 'ASC',
   'meta_query' => array(
           'key' => 'age',
           'value' => array(3, 4),
           'compare' => 'IN',
 $query = new WP_Query($args);

You could just pass your $category as is but I'd filter it for maliciousness. In fact, your values are (or seem to be) static so the safest thing is to do this:

$category_args = array();
if(!empty($category[0])) $category_args[] = "Planet";
if(!empty($category[1])) $category_args[] = "People";

And pass that into your query like so:

if( !empty($category_args) ){
      $argsearch["meta_query"] = array();
  $argsearch["meta_query"][] = array(
      'key' => '_cmb_project_category',
      'value' => $category_args,
      'compare' => 'IN'
share|improve this answer
Thank you @s_ha_dum problem solved. +1 – iDev Man Nov 4 '12 at 16:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.