2

Say I have a bunch of posts, some of them have a meta key called "key1" and some of them have a meta key called "key2". But the posts do not have both.

I'd like to create a custom query which retrieves any post that has "key1" or "key2" as a meta key. (not a single post which has both).

After digging around, the closest I've got to (for a single query) was using the meta_query array which allows me to query for posts that have multiple meta keys. However I am looking for posts OF multiple meta keys.. ie: if it has key1 or key2, load up the query.

Aside from the above, the only other way I can seem to query for posts that have either key1 or key2 is to create multiple queries and loops. I'm wondering if there's a cleaner solution. My ideal end-result would be to have the resulting post mixed in with each other and displayed as if it came from a single query. With multiple queries I feel I wont be able to achieve this as I will get the results with key1 first and then the results with key2 afterwards in the next query/loop.

Thanks in advance!

4

An easy query like the following should work for you:

<?php
$_query = new WP_Query( array(
        'post_type'         => 'post',
        'posts_per_page'    => -1,
        'post_status'       => 'publish',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key'     => 'key1'
            ),
            array(
                'key'     => 'key2'
            ),
        ),
    ) );
?>

Note the 'relation'=>'OR' in meta_query.

More at:

| improve this answer | |
  • @PieterGoosen I'd like to create a custom query which retrieves any post that has "key1" or "key2" as a meta key. - isn't that the actual query? – Mayeenul Islam Sep 3 '15 at 7:32
  • 1
    This is it! This worked PERFECTLY. It queries the DB for any post with EITHER (or) key1 or key2. If the post has either one of those keys, load up the query object. I'm actually stunned at how simple the solution was. I spent hours last night trying to figure it out and my script looking almost identical. The golden egg here was adding "'relation' => 'OR'," inside of the meta_query array before the meta key arrays. I had no idea you could do this and I after reading the codex I wasnt able to find this documented anywhere. Thanks so much Mayeenul! – hyp0xia Sep 3 '15 at 18:03
  • 1
    @MayeenulIslam seemed I was the one misreading the question. :-) +1 – Pieter Goosen Sep 3 '15 at 18:32
0

You could try adding a few more checks in your meta_query.

$args = array(

    'post_type'   => 'post',
    'posts_per_page' => 10,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'relation' => 'AND', // make sure only this key exists by matching parameters
            array(
                'key' => 'key1',
                'compare' => 'EXISTS'
            ),
            array(
                'key' => 'key2',
                'compare' => 'NOT EXISTS'
            )
       ),
       array(
           'relation' => 'AND', // check for the opposite here
            array(
                'key' => 'key2',
                'compare' => 'EXISTS'
            ),
            array(
                'key' => 'key1',
                'compare' => 'NOT EXISTS'
            )
        )
    )
);

This is an ok solution if you're only comparing two meta keys, but it might be cleaner to write a sql query.

| improve this answer | |

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.