Get the the top-level parent of a custom taxonomy term

Thanks to Ivaylo for this code, which was based on Bainternet’s answer. The first function below, get_term_top_most_parent, accepts a term and taxonomy and returns the the term’s top-level parent (or the term itself, if it’s parentless); the second function (get_top_parents) works in the loop, and, given a taxonomy, returns an HTML list of the top-level … Read more

Change order of Custom Taxonomy List

This isn’t possible “out of the box”… The default ‘orderby’ options are (ascending or descending) ID name Default slug count term_group These are all detailed in the codex. — That said there are some clever ladies & gents here. If anyone can solve it, one of these guys can i’m sure!

Get terms by taxonomy AND post_type

Here is another way to do something similar, with one SQL query: static public function get_terms_by_post_type( $taxonomies, $post_types ) { global $wpdb; $query = $wpdb->prepare( “SELECT t.*, COUNT(*) from $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p … Read more

Retrieve posts by term id custom query

Have you tried using the WP_Query class? You might find it’s easier to use the built-in tools for this instead of a custom query from scratch. Something similar to the following should work for you: <?php $args = array( ‘post_type’ => ‘recipe_cpt’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘recipe_tx’, ‘field’ => ‘term_id’, ‘terms’ => 37 … Read more

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/