WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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',
        'meta_key' => '_homeslide', 
        'meta_value' => '1'
        '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.

share|improve this question
Your query cannot be created using any native WP query method. You'll need to resort to custom SQL or to modify a query using filters. – Otto Oct 14 '11 at 19:53

For that kind of complex query, I'd recommend running two queries and then merging the results.

First, get an array for:

get all posts WHERE ( post_type = 'post' AND category = 7 )

Then, get an array for:

( post_type = 'case-studies' AND meta_key = 'homeslide' AND meta_value = 1 )

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.

share|improve this answer
Will this retain the correct order by date though? – digout Oct 14 '11 at 18:33
You didn't have anything about ordering in your original question. Please add all the details. But no, merging the arrays will not preserve a date order. – EAMann Oct 14 '11 at 18:42
up vote 0 down vote accepted

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.

// Collect posts
$news = get_posts(); 
$cs = query_posts( array(
    'post_type' => 'case-studies',
    'meta_key' => '_homeslide',
    'meta_value' => 'true'

// Merge the two arrays
$vslides = array_merge($news, $cs);

// Function to compare array elements using post_date
function compare($x, $y) {
    if ( strtotime($x->post_date) == strtotime($y->post_date) )
        return 0;
    else if ( strtotime($x->post_date) > strtotime($y->post_date) )
        return -1;
        return 1;

// Sort by post_date
uasort($vslides, 'compare');
share|improve this answer

Your Answer


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.