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.

Trying to run a query that pulls:

  • get me 4 posts or pages that are not in sticky_posts and have (my_featured_post = 1 AND photo-image_post_id > 0) OR (my_featured_post = 1 AND photo-image_page_id > 0)

  • OR if it's easier to do - somehow saying (my_featured_post = 1) AND (photo-image_page_id>0 OR photo-image_post_id >0)

I tried

$query = new WP_Query(
    array(
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'my_featured_post', 
                'value' => '1', 
                'compare' => '='
            ), 
            array(
                'key' => 'photo-image_post_id', 
                'value' => '0', 
                'compare' => '>'
            ),
            array(
                'key' => 'my_featured_post', 
                'value' => '1', 
                'compare' => '='
            ), 
            array(
                'key' => 'photo-image_page_id', 
                'value' => '0', 
                'compare' => '>'
            ),
        ),
        'post_type' => array( 'post', 'page' ),
        'post__not_in' =>get_option('sticky_posts'),
        'posts_per_page'=> 4,        
   )
);

This does not seem to work. What should I do?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I don't think you'll be able to do this in a single query - what you seem to need is multiple relationships between conditions, and as it works currently, meta_query doesn't let you do that. I googled it a bit and found others saying it doesn't, but I also went into the core code and looked at the WP_Meta_Query class. I can tell you definitively, you can't create multiple relations the way you need.

What I would suggest is to run a query that just grabs posts with my_featured_post = 1, as a meta query, and then get all the ids of those posts. Then run another query passing those ids to post__in - and running a meta query one for photo-image_page_id>0 OR photo-image_post_id >0

share|improve this answer

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.