You should use ‘terms’ instead of ‘term’ in the tax_query. You’re missing the ‘s’.
Typical, I thought it’d be something daft like that! Thanks for the sanity check 🙂
Updated to:
'tax_query' => array(
array(
'taxonomy' => 'talk-type',
'field' => 'slug',
'terms' => 'sunday-talk'
)
)
Tax Query still isn’t returning any posts, unfortunately. The term is definitely in the database.
Try terms as an array and use an operator of IN
'tax_query' => array(
array(
'taxonomy' => 'talk-type',
'field' => 'slug',
'terms' => array('sunday-talk'),
'operator' => 'IN'
)
)
Still returns no posts, I’m afraid!
Just to clarify a few points in case it helps:
The database has a slug of “sunday-talk” in the wp_terms
table with a term_id
of 8. The term_group
is 0, though I’m not sure what this refers to.
The wp_term_taxonomy
table shows a term_taxonomy_id
and term_id
of 8, in the taxonomy “talk-type”, with a count
of 5.
The wp_term_relationships
table shows each of those 5 object_id
entries with a term_taxonomy_id
of 8.
So from the database side, everything looks like it’s being added and associated properly.
My full WP_Query
is:
$nextSundayTalkArgs = array(
'post_type' => 'talk',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'talk-type',
'field' => 'slug',
'terms' => array('sunday-talk'),
'operator' => 'IN'
)
)
);
$nextSundayTalkQuery = new WP_Query( $nextSundayTalkArgs );
and works just fine if I comment out the tax_query
.
Can’t spot anything that would be causing a problem, but this is my first week of WP development so I’m still pretty new!
Thanks for your continued help.
Are you sure you have some posts of type “talk” that have a taxonomy of ‘sunday-talk’ set.
Hi Bruce,
As far as I can see, yes. The information I posted above about what’s in the database tables seems to indicate that the posts have the correct taxonomy term.
I’m not sure why it’s not working.
Try echoing the following to see what the generated SQL is:
$nextSundayTalkQuery->request
It might give a clue.
If the information from my database tables is somehow wrong, and posts aren’t being allocated the terms properly, is there some way I can clear the taxonomy / terms out of the database fully, and start again to be sure?
I got the following (though I should warn you I’m extremely inexperienced with MySQL):
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND 0 = 1 AND wp_posts.post_type = 'talk' AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND ( (wp_postmeta.meta_key = 'talk-details-date' AND CAST(wp_postmeta.meta_value AS CHAR) >= '1359023830') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC LIMIT 0, 1
I should probably mention that there’s a date comparison in there now (as it’s coming up in the MySQL). I updated the query to include some date-based filtering:
$nextSundayTalkArgs = array(
'meta_query' => array(
array(
'compare' => '>=',
'key' => 'talk-details-date',
'value' => date('U', time())
)
),
'order' => 'ASC',
'orderby' => 'meta_value',
'post_type' => 'talk',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'talk-type',
'field' => 'slug',
'terms' => array('sunday-talk'),
'operator' => 'IN'
)
)
);
$nextSundayTalkQuery = new WP_Query( $nextSundayTalkArgs );
But it still works if I remove the tax_query
, and the tax_query
still doesn’t work if I comment out the date filtering, so adding it shouldn’t have been a problem.
petegale, this seems not directly related to Types support, as it’s more of a generic WordPress query support question. I’m marking it as resolved, as there isn’t anything that we need to update in Types.
A little disappointing, as the only issue I’m having is with Types, so for me it seems related. The rest of my query works perfectly until I try and query the taxonomy (which I’ve created through Types).
I guess it’s time to try a different plugin.
Maybe it’s related to the order of execution. Types defines these relationships in the standard WordPress init event. The only thing that comes to mind is maybe your code also runs in the init, and does so before Types?