0

I'm using a meta_query with a relation 'OR' with two keys to retrieve all tags and it working perfectly

$args = array(
    'taxonomy'   => 'post_tag',
    'hide_empty' => true,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'   => 'key-check',
            'value' => false,
        ),
        array(
            'key'     => 'key-check',
            'compare' => 'NOT EXISTS',
        ),
    ),
);

I have to add another different key but I don't know-how is the best way to do it. I thought to use the below code and add another meta_query but it's correct or I'm making an error?

$args = array(
    'taxonomy'   => 'post_tag',
    'hide_empty' => true,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'   => 'key-check',
            'value' => false,
        ),
        array(
            'key'     => 'key-check',
            'compare' => 'NOT EXISTS',
        ),
    ),
    'meta_query' => array(
        array(
            'key'   => 'another-key',
            'value' => true,
        ),
    ),
);
C. Max
  • 45
  • 1
  • 7
  • These are not "multiple meta_queries". It's the same meta_query with multiple keys. – Khom Nazid Apr 10 '22 at 14:34
  • Forget about meta queries or wordpress. This one is about PHP. If you declare an array and use the same key twice, the last one overwrites all other entries. So your array is equivalent to this: `array( 'taxonomy' => 'post_tag', 'hide_empty' => true, 'meta_query' => array(array('key' => 'another-key', 'value' => true,)));`. The first meta_query entry is being completely ignored. – Bruno Polo Jun 27 '22 at 14:01

2 Answers2

2

You can use multiple meta query & skip empty values:

$arg = [
    'post_type'     => 'post',
    'status'        => ['publish'],
    'post_per_page' => -1,
];

$arg['meta_query'] = [
    'relation' => 'OR',
    [
        'key' => 'custom_1',
        'value' => '',
        'compare' => '!='
    ],
    [
        'key' => 'custom_2',
        'value' => '',
        'compare' => '!='
    ],
];

1

You are using the same "meta_query" key twice that's why the issue is creating. check the below code.

$args = array(
    'taxonomy'   => 'post_tag',
    'hide_empty' => true,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'   => 'key-check',
            'value' => false,
        ),
        array(
            'key'     => 'key-check',
            'compare' => 'NOT EXISTS',
        ),
        array(
            'key'   => 'another-key',
            'value' => true,
        ),
    ),
);
Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • Thank you so much. Except for my question have the sense or is possible to use two 'meta_query' if there are other different conditions? – C. Max Nov 09 '20 at 13:44
  • 1
    No, but you can check this answer here [Nested meta_query with multiple relation keys](https://wordpress.stackexchange.com/questions/75079/nested-meta-query-with-multiple-relation-keys) if there are other different conditions check this https://wordpress.stackexchange.com/a/310720/127648 – Bhautik Nov 09 '20 at 13:45
  • that's perfect, thanks so much for sharing these I haven't seen them before wrote here – C. Max Nov 09 '20 at 13:53