get_terms by custom post type

I’m afraid this isn’t possible natively (yet?). See this trac: http://core.trac.wordpress.org/ticket/18106 Similarly on the taxonomy admin page the post count reflects all post types. (I’m pretty sure there is a trac ticket for that too) http://core.trac.wordpress.org/ticket/14084 See also, this related post. New solution Having written the one below, I’ve released a much better way (alteast … Read more

How can I get only parent terms?

Yes, just pass in the parent parameter to get_terms when you call it, as Michael pointed out. Since WP 4.5 this is the recommend usage: $myterms = get_terms( array( ‘taxonomy’ => ‘taxonomy_name’, ‘parent’ => 0 ) ); Prior to WP 4.5 this was the default usage: $myterms = get_terms( ‘taxonomy_name_here’, array( ‘parent’ => 0 ) … Read more

get_posts assigned to a specific custom taxonomy term, and not the term’s children

In looking at the WP_Tax_Query class in /wp-includes/taxonomy.php, I found that there is a ‘include_children’ option which defaults to true. I modified my original get_posts() call with the following, and it works great: $pages = get_posts(array( ‘post_type’ => ‘page’, ‘numberposts’ => -1, ‘tax_query’ => array( array( ‘taxonomy’ => ‘taxonomy-name’, ‘field’ => ‘term_id’, ‘terms’ => 1, … Read more

How to get a taxonomy term name by the slug?

The function you are looking for is get_term_by. You would use it as such: <?php $term = get_term_by(‘slug’, ‘my-term-slug’, ‘category’); $name = $term->name; ?> This results in $term being an object containing the following: term_id name slug term_group term_taxonomy_id taxonomy description parent count The codex does a great job explaining this function: https://developer.wordpress.org/reference/functions/get_term_by/

Show all terms of a custom taxonomy?

You need to pass an additional argument to get_terms(). The default is to hide “empty” terms– terms which are assigned to no posts. $terms = get_terms([ ‘taxonomy’ => $taxonomy, ‘hide_empty’ => false, ]); EDIT: Incase you want to display the name or the slug of the enlisted custom taxonomies being held by the $terms variable … Read more

How to show a hierarchical terms list?

Use wp_list_categories with the ‘taxonomy’ => ‘taxonomy’ argument, it’s built for creating hierarchical category lists but will also support using a custom taxonomy.. Codex Example: Display terms in a custom taxonomy If the list comes back looking flat, it’s possible you just need a little CSS to add padding to the lists, so you can … Read more

Is There a Difference Between Taxonomies and Categories?

Taxonomies, as previously described are a collective noun for the following category post_tag post_format link_category custom taxonomy The first four are built-in taxonomies, while custom taxonomies are taxonomies that are manually created by the user with register_taxonomy. Custom Taxonomies can be hierarchical (like the build-in taxonomy category) or not (like post tags) The categories and … Read more