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 am trying to create a widget which queries multiple taxonomies for my plugin but am having problems with my query.

My query is:

    $query_args = array(
        'numberposts' => $limit,
        'order' => 'DESC',
        'orderby' => 'post_date',
        'post_type' => 'match',
        'posts_per_page' => $limit,
    );

    $query_args['meta_query'] = array(
        'key' => 'played',
        'value' => true,
    );

    $query_args['meta_query'] = array(
                    'relation' => 'OR',
        array(
            'key' => 'home_club',
            'value' => $club,
        ),
        array(
            'key' => 'away_club',
            'value' => $club,
        ),
    );

    $query_args['tax_query'] = array(
        array(
            'taxonomy' => 'comp',
            'terms' => $comp,
            'field' => 'term_id'
        ),
        array(
            'taxonomy' => 'season',
            'terms' => $season,
            'field' => 'term_id'
        ),
        array(
            'taxonomy' => 'team',
            'terms' => $team,
            'field' => 'term_id'
        ),
        array(
            'taxonomy' => 'venue',
            'terms' => $venue,
            'field' => 'term_id'
        ),
    );

    $r = new WP_Query( $query_args );

    if ( $r->have_posts() ) {

        while ( $r->have_posts()) {
        $r->the_post();
            ...
        }
    }

I'm 99.9% sure that the taxonomies are correctly registered as they are in use in many other areas of the site including a similar widget using get_posts.

The above query outputs nothing, as if there are no matches. If I add 'relation' => 'OR', to the tax_query it shows a list of matches but without the taxonomy filtering, the same happens if I remove the tax_query completely.

The following SQL query is generated:

SELECT SQL_CALC_FOUND_ROWS hoc_posts.ID 
FROM hoc_posts 
INNER JOIN hoc_term_relationships 
ON (hoc_posts.ID = hoc_term_relationships.object_id) 
INNER JOIN hoc_term_relationships AS tt1 
ON (hoc_posts.ID = tt1.object_id) 
INNER JOIN hoc_term_relationships AS tt2 
ON (hoc_posts.ID = tt2.object_id) 
WHERE 1=1 
AND ( hoc_term_relationships.term_taxonomy_id IN (17) OR tt1.term_taxonomy_id IN (3) OR tt2.term_taxonomy_id IN (4) ) 
AND hoc_posts.post_type = 'match' 
AND (hoc_posts.post_status = 'publish' OR hoc_posts.post_status = 'private') 
GROUP BY hoc_posts.ID 
ORDER BY hoc_posts.post_date DESC LIMIT 0, 6

And the result of print_r( $query_args ); is:

Array ( 
    [numberposts] => 6 
    [order] => DESC 
    [orderby] => post_date 
    [post_type] => match 
    [posts_per_page] => 6 
    [meta_query] => Array ( 
        [key] => played 
        [value] => 1 
    ) 
    [tax_query] => Array ( 
        [relation] => OR 
        [0] => Array ( 
        [taxonomy] => comp 
        [terms] => 17 
        [field] => term_id 
        ) 
        [1] => Array ( 
        [taxonomy] => season 
        [terms] => 3 
        [field] => term_id 
        ) 
        [2] => Array ( 
       [taxonomy] => team 
        [terms] => -1 
        [field] => term_id 
        ) 
        [3] => Array ( 
        [taxonomy] => venue 
        [terms] => 4 
        [field] => term_id 
        ) 
    ) 
)

I'm pulling my hair out trying to figure out the problem. I've searched through several related posts on this site and SO but none seem to have solved my particular problem.

Any help would be greatly appreciated. :)

share|improve this question
1  
Looking at your dump -1 team ID seems suspicious. –  Rarst May 29 '14 at 14:13
    
Thanks for the reply but I don't think that's the problem. Even if I manually change that or remove the team query, the problem persists. –  roofdog May 29 '14 at 14:21
    
Saying that, I've just changed the 'terms' to null in my query for teams, venues and seasons and now the comps term is working as it should. –  roofdog May 29 '14 at 14:24
    
I'm still bamboozled! It's only displaying something if I use relation => OR for my tax_query but I don't want to use OR. This is really confusing me :s –  roofdog May 29 '14 at 14:33
    
You should add your edit as an answer as it seems that solved your problem. This will not only keep your question constructive to the site, but it can score you some reputation points and a couple of badges –  Pieter Goosen May 29 '14 at 18:39

1 Answer 1

up vote 0 down vote accepted

Thanks to Rarst for pointing me in the right direction. After a bit of playing around the following query is now producing the effect I'm after:

$query_args = array(
    'numberposts' => $limit,
    'order' => 'DESC',
    'orderby' => 'post_date',
    'post_type' => 'match',
);

$query_args['meta_query'] = array(
    'key' => 'played',
    'value' => true,
);

if ( isset( $comp ) )
    $query_args['tax_query'][] = array(
        'taxonomy' => 'comp',
        'terms' => $comp,
        'field' => 'term_id',
    );

if ( isset( $season ) )
    $query_args['tax_query'][] = array(
        'taxonomy' => 'season',
        'terms' => $season,
        'field' => 'term_id',
    );

if ( isset( $team ) )
    $query_args['tax_query'][] = array(
        'taxonomy' => 'team',
        'terms' => $team,
        'field' => 'term_id',
    );

if ( $venue > 0  )
    $query_args['tax_query'][] = array(
        'taxonomy' => 'venue',
        'terms' => $venue,
        'field' => 'term_id',
    );
share|improve this answer

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.