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.

I have created a custom post type with custom fields and I need to filter them based on several meta data values.

$args = array(
       'post_type' => 'match_report',
       'post_status' => 'publish',
       'meta_key' => 'report_type',
       'meta_value' => 'cup',
       'meta_query' => array(
           'relation' => 'OR',
              array(
                 'key' => 'report_home-select',
                 'value' => $team_id,
                 'compare' => '=',
              ),
              array(
                 'key' => 'report_away-select',
                 'value' => $team_id,
                 'compare' => '='
              )
       )                                  
     );

    $reports = new WP_Query($args);

Basically I want to create a meta query that has a relation of OR and AND. So in English I want the all the match reports with the report_type 'cup' that have either the report_home-select key with the value of a specific team_id OR the report_away-select key with the same value. I hope that makes sense. In essence I want a double relation.

share|improve this question
add comment

1 Answer

You could split this into two WP_Querys:

 $args = array(
   'post_type' => 'match_report',
   'post_status' => 'publish',
   'meta_key' => 'report_type',
   'meta_value' => 'cup',
   'fields' => 'ids'                                 
 );

$reportsPre = new WP_Query($args);
$post_IDs = array();
if(isset($reportsPre->posts) && !empty($reportsPre->posts)){
  foreach((array) $reportsPre->posts as $id) {
    $post_IDs[] = $id;
  }
}

$args2 = array(
   'post__in' => array($post_IDs),
   'meta_query' => array(
       'relation' => 'OR',
          array(
             'key' => 'report_home-select',
             'value' => $team_id,
             'compare' => '=',
          ),
          array(
             'key' => 'report_away-select',
             'value' => $team_id,
             'compare' => '='
          )
   ));

  $reports = new WP_Query($args2);

I'm not near a computer where I could test this but it should work.

share|improve this answer
    
It didn't but I see what you mean. So I have to use a nested query of sorts? I'll see if I can get it to work. –  mantis Jun 25 '13 at 17:08
    
Any luck? have you tried using LIKE as the compare operator? –  eteich Jun 25 '13 at 17:56
    
I did but no luck :( I decided to create custom taxonomies in the end. Works like a charm now but I am still curious about how to solve the original problem. Thanks for your help. –  mantis Jun 25 '13 at 21:04
add comment

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.