0

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) {

    $i++; 

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

}

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

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

$query = new WP_Query( $args );

It gives the right result:

api.php?key=fruit,drink&value=cherry,whiskey

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

api.php?key=fruit,drink&value=cherry

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

api.php?key=fruit,drink&value=any,whiskey

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

0

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:

api.php?key=fruit,drink&value=,whiskey

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.