• Hi there,

    I’ve been using the Types plugin for a short while now. I created a custom taxonomy called “Talk Type”, with the slug “talk-type”. The taxonomy is showing up in the database as “talk-type”, but I can’t seem to check it in a WP_Query.

    I’ve tried simply:

    'talk-type'=>'sunday-talk'

    and I’ve also tried:

    'tax_query' => array(
     array(
      'taxonomy' => 'talk-type',
      'field' => 'slug',
      'term' => 'sunday-talk'
     )
    )

    Neither work. The first (using 'taxonomy-name'=>'term-slug') just seems to ignore the taxonomy, and show me all posts from that post type. The second (tax_query) shows no posts at all.

    Can anyone offer some suggestions please?

    Thanks!

    http://wordpress.org/extend/plugins/types/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Contributor brucepearson

    (@brucepearson)

    You should use ‘terms’ instead of ‘term’ in the tax_query. You’re missing the ‘s’.

    Thread Starter petegale

    (@petegale)

    Typical, I thought it’d be something daft like that! Thanks for the sanity check 🙂

    Thread Starter petegale

    (@petegale)

    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.

    Plugin Contributor brucepearson

    (@brucepearson)

    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'
     		)
    	)

    Thread Starter petegale

    (@petegale)

    Still returns no posts, I’m afraid!

    Thread Starter petegale

    (@petegale)

    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.

    Plugin Contributor brucepearson

    (@brucepearson)

    Are you sure you have some posts of type “talk” that have a taxonomy of ‘sunday-talk’ set.

    Thread Starter petegale

    (@petegale)

    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.

    Plugin Contributor brucepearson

    (@brucepearson)

    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.

    Thread Starter petegale

    (@petegale)

    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?

    Thread Starter petegale

    (@petegale)

    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

    Thread Starter petegale

    (@petegale)

    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.

    Plugin Author Amir Helzer

    (@amirhelzer)

    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.

    Thread Starter petegale

    (@petegale)

    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.

    Plugin Author Amir Helzer

    (@amirhelzer)

    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?

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Custom Taxonomies in WP_Query’ is closed to new replies.