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?
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 topost__in
– and running a meta query one forphoto-image_page_id>0 OR photo-image_post_id >0