You code is generating a broken meta_query
. You code will produce something like this:
array(1) {
["meta_query"]=>
array(2) {
[0]=>
array(2) {
["relation"]=>
string(2) "OR"
[0]=>
array(3) {
["key"]=>
string(4) "test"
["value"]=>
string(10) "test_value"
["compare"]=>
string(4) "LIKE"
}
}
}
It should look like this:
array(1) {
["meta_query"]=>
array(2) {
["relation"]=>
string(2) "OR"
[0]=>
array(3) {
["key"]=>
string(4) "test"
["value"]=>
string(10) "test_value"
["compare"]=>
string(4) "LIKE"
}
}
}
The problem is the trailing square brackets here: $args['meta_query'][]
That is incorrect. To combine your values do something like this instead:
$meta_q = array();
$key ='test';
$value="test_value";
if (!empty($key) && !empty($value)) {
$meta_q[] = array(
'key' => $key,
'value' => $value,
'compare' => 'LIKE'
);
}
$key2 ='test';
$value2='test_value';
if (!empty($key2) && !empty($value2)) {
$meta_q[] = array(
'key' => $key2,
'value' => $value2,
'compare' => 'LIKE'
);
}
$args = array(
'relation' => 'OR',
'meta_query' => $meta_q,
);
var_dump($args);