I am trying to use wp_query (or another native WordPress query class) to do the following:
get all posts WHERE
( post_type = ‘post’ AND category = 7 ) OR
( post_type = ‘case-studies’ AND meta_key = ‘homeslide’ AND meta_value = 1 )
I am not sure any of them can handle this yet.
Trying with WP Query:
$args = array (
'relation' => 'OR',
array(
'meta_key' => '_homeslide',
'meta_value' => '1'
),
array(
'category' => '7'
)
);
But I think “relation” is only for use with tax_query.
I could probably use $wpdb to create an advanced query but wanted to see if it could be done with the others and see if I was missing something obvious.
For that kind of complex query, I’d recommend running two queries and then merging the results.
First, get an array for:
Then, get an array for:
Then use
array_merge()
to combine the arrays and and you have a collection that satisfies your requirements. Yes, you’re doing to queries … but this seems like a fairly quick/easy short-term solution.Ok I put together a solution using get_posts & query_posts to return the arrays for each: posts and case-studies. Then array_merged them (thanks for the heads up EAMann), then using uasort() with a custom compare function to sort them by post_date.
It’s not the most attractive solution, but since it works and I’m only returning a max of 5 posts in total from each query, it’ll do.