tax_query not working
-
First off – great plugin! My problem is that tax_query isn’t working for me (WIP site is https://sar.sunriseweb.ca ). I checked the database and the term_relationships table doesn’t contain any entries linking the object_id (i.e. post_id for my custom post type) and term_taxonomy_id (i.e. the cpt-onomy taxonomy post_id).
So… when I try the following query I get no results:
$postQueryArgs = array( 'post_type' => 'contacts', 'nopaging' => true, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'tax_query' => array( array( 'taxonomy' => 'countries', 'field' => 'id', 'terms' => $termone->term_id, ) ) ); $postquery = new WP_Query( $postQueryArgs );
Looking at the generated SQL I can see this is because there is no data in the term_relationships table. Any ideas why?
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id ) INNER JOIN wp_postmeta AS cpt_onomy_pm1 ON ( wp_posts.ID = cpt_onomy_pm1.post_id AND cpt_onomy_pm1.meta_key = '_custom_post_type_onomies_relationship' ) WHERE 1 =1 AND ( wp_term_relationships.term_taxonomy_id IN ( 95 ) ) AND wp_posts.post_type = 'contacts' AND ( wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private' ) AND ( cpt_onomy_pm1.meta_value IN ( 95 ) ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC LIMIT 0
What I’m doing to work around this is using a meta_query, and this is working for now:
$postQueryArgs = array( 'post_type' => 'contacts', 'nopaging' => true, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'meta_query' => array( array( 'key' => '_custom_post_type_onomies_relationship', 'value' => array(intval($_POST['countryid'])), 'compare' => 'IN', ) ) ); $postquery = new WP_Query( $postQueryArgs );
Thanks in advance.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘tax_query not working’ is closed to new replies.