Query by multiple meta elements not seeming to work – wordpress is timing out

Trying to run a query that pulls:

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?

Related posts

Leave a Reply

1 comment

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