Meta_query with multiple keys and multiple values

Question

I have a WP Query code like below to check if have post match all post_meta > get that post, else create new post, but the result always show me a post have only loaicua = cua-chinh and loaiphong = phong-khach and i don’t know why @@

Can someone point out the error in the code below? I will appreciate any help.

Thank you.

foreach( $meta_array as $meta ) { $i++;
        if (!isset($page_query_args[$meta['key']])) {
            $page_query_args[$meta['key']]['key'] = $meta['key'];
            $page_query_args[$meta['key']]['value'][] = $meta['value'];
            $page_query_args[$meta['key']]['type'] = 'CHAR';
            $page_query_args[$meta['key']]['compare'] = 'IN';
        } else {
            $page_query_args[$meta['key']]['value'][] = $meta['value'];
        }
        if( $i == sizeof( $meta_array ) ) {
            $page_query_args = array_values($page_query_args);
        }
    }

if( sizeof( $meta_array ) > 1 ) {
        $page_query_args['relation'] = 'AND';
    }

$page_query = new WP_Query(
        array(
            'post_type' => 'page',
            'orderby' => 'date',
            'order' => 'DESC',
            'meta_query' => $page_query_args,
        )
    );

Output of $page_query_args

array(3) {
  [0]=>
  array(4) {
    ["key"]=>
    string(7) "loaicua"
    ["value"]=>
    array(1) {
      [0]=>
      string(9) "cua-chinh"
    }
    ["type"]=>
    string(4) "CHAR"
    ["compare"]=>
    string(2) "IN"
  }
  [1]=>
  array(4) {
    ["key"]=>
    string(9) "loaiphong"
    ["value"]=>
    array(3) {
      [0]=>
      string(11) "phong-khach"
      [1]=>
      string(9) "phong-ngu"
      [2]=>
      string(12) "phong-tre-em"
    }
    ["type"]=>
    string(4) "CHAR"
    ["compare"]=>
    string(2) "IN"
  }
  ["relation"]=>
  string(3) "AND"
}
0
Nguyen Van Duc 3 months 0 Answers 10 views 0

Leave an answer