Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
Thank you Otto. I will do it in custom SQL. –  Olsi Dec 6 '11 at 22:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.