I am have this following API code for $_REQUEST

$metakey        = explode(',',$wpdb->escape($_REQUEST['key']) );
$metavalue      = explode(',',$wpdb->escape($_REQUEST['value']) );

$meta = array();

$i = -1;

foreach ($metakey as $key) {


    if( $metavalue[$i] && trim($metavalue[$i]) != "")
        $meta[] = array( 'key' => $key, 'value' => $metavalue[$i] );
        $meta[] = array( 'key' => $key );


$meta['relation'] = 'AND';

$args = array();
$args['meta_query'] = $meta;

$query = new WP_Query( $args );

It gives the right result:


It gives the right result for fruit -> cherry and drink -> any:


But this program do not for fruit -> any and drink -> whiskey:


How to pass the meta_value empty value or "any" string?


You don’t have any conditions for “any” as a given value in your code, so it won’t be treated as any, but as “any”.

But your code checks if given value is not empty, so passing it like so should work:


PS. I’m not sure if that’s the best way of passing arguments to your api ;)

  • Thanks, if($metavalue[$i]&&trim($metavalue[$i])!=""&&$metavalue[$i]!="any") it is resolve.
    – l6ls
    Apr 27 '18 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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