WordPress.org

Ready to get started?Download WordPress

Forums

Types - Custom Fields and Custom Post Types Management
Custom Taxonomies in WP_Query (16 posts)

  1. petegale
    Member
    Posted 1 year ago #

    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/

  2. brucepearson
    Member
    Plugin Author

    Posted 1 year ago #

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

  3. petegale
    Member
    Posted 1 year ago #

    Typical, I thought it'd be something daft like that! Thanks for the sanity check :)

  4. petegale
    Member
    Posted 1 year ago #

    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.

  5. brucepearson
    Member
    Plugin Author

    Posted 1 year ago #

    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'
     		)
    	)
  6. petegale
    Member
    Posted 1 year ago #

    Still returns no posts, I'm afraid!

  7. petegale
    Member
    Posted 1 year ago #

    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.

  8. brucepearson
    Member
    Plugin Author

    Posted 1 year ago #

    Are you sure you have some posts of type "talk" that have a taxonomy of 'sunday-talk' set.

  9. petegale
    Member
    Posted 1 year ago #

    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.

  10. brucepearson
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  11. petegale
    Member
    Posted 1 year ago #

    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?

  12. petegale
    Member
    Posted 1 year ago #

    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

  13. petegale
    Member
    Posted 1 year ago #

    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.

  14. AmirHelzer
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  15. petegale
    Member
    Posted 1 year ago #

    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.

  16. AmirHelzer
    Member
    Plugin Author

    Posted 1 year ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.