1

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,
        ),
    ),
);
2
  • These are not "multiple meta_queries". It's the same meta_query with multiple keys. Commented Apr 10, 2022 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. Commented Jun 27, 2022 at 14:01

2 Answers 2

3

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' => '!='
    ],
];

Sign up to request clarification or add additional context in comments.

Comments

2

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,
        ),
    ),
);

3 Comments

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?
No, but you can check this answer here Nested meta_query with multiple relation keys if there are other different conditions check this wordpress.stackexchange.com/a/310720/127648
that's perfect, thanks so much for sharing these I haven't seen them before wrote here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.