5

I'm trying to do some filtering for some posts in WP_Query, but I can't seem to get the right condition. Basically what I wanna do, is get posts that have the custom field event_data (sorted by this field) and among those posts to select only those who have one of these other fields filled in event_doc1, event_doc2, event_doc3, main_doc.

The code is below.

$metaCondition = array(
            'relation' => 'OR',
            array(
                'key' => 'main_doc',
                'value' => 0,
                'compare' => '!=',
                'type' => 'NUMBER'
            ),
            array(
                'key' => 'event_doc1',
                'value' => 0,
                'compare' => '!=',
                'type' => 'NUMBER'
            ),
            array(
                'key' => 'event_doc2',
                'value' => 0,
                'compare' => '!=',
                'type' => 'NUMBER'
            ),
            array(
                'key' => 'event_doc3',
                'value' => 0,
                'compare' => '!=',
                'type' => 'NUMBER'
            )
        );

        $query = array(
            'numberposts' => 4, 
            'orderby' => 'meta_value',
            'meta_key' => 'event_data', 
            'order' => 'ASC', 
            'meta_query' => $metaCondition
        );

Thanks in advance to everone!

7

Sorry, doesn't work that way. The "simple" meta parameters are converted into part of the meta_query as a whole. So it's not a separate thing, and your OR relation applies to it as well.

Your query thus is not possible with the normal query system, because you want to mix ANDs with ORs (you want items that have event_data ANDed with a bunch of others with ORs between them).

You'll have to resort to either custom SQL or rethink your design.

| 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.