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. :)
-1
team ID seems suspicious.null
in my query for teams, venues and seasons and now the comps term is working as it should.