Correct use of get_the_terms()

the_ID() print the post ID. You need to use the get_the_ID() which return the post ID. Example: foreach (get_the_terms(get_the_ID(), ‘taxonomy’) as $cat) { echo $cat->name; } Always remember the naming convention of WordPress for template tags. the which mean to print get which mean to return in most of the cases.

When to / not to use wp_get_post_terms vs get_the_terms?

A user contributed a note in the codex saying the only difference is that get_the_terms use cached data Yes, get_the_terms relies on the object cache. This gives a scaling boost and a speed boost. If you have an object cache enabled this boost increases speed dramatically. and I already know that wp_get_post_terms let you retrieve … Read more

How to get_term_children output in alphabetical order?

get_term_children() only outputs the term IDs, and you later get details for each term using get_term_by(). You can combine these queries in one using get_terms() with the child_of argument: get_terms( $taxonomyName, array( ‘child_of’ => $termID ) ); By default this sorts by name. However, it is possible that the child_of argument undoes the sorting. In … Read more

Get parent id by term id

If you already have the term, like the term is an actual object you could use $term->parent. Otherwise you can do something like this: $term = get_term($id, ‘YOUR_TAXONOMY_HERE’); $termParent = ($term->parent == 0) ? $term : get_term($term->parent, ‘YOUR_TAXONOMY_HERE’); The 2nd portion of this is a shorthand if-else, IF it doesn’t have a parent, then we … Read more

Display all the subcategories from a specific category?

Try something like this: by ID function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) { $args = array( ‘hierarchical’ => 1, ‘show_option_none’ => ”, ‘hide_empty’ => 0, ‘parent’ => $parent_cat_ID, ‘taxonomy’ => ‘product_cat’ ); $subcats = get_categories($args); echo ‘<ul class=”wooc_sclist”>’; foreach ($subcats as $sc) { $link = get_term_link( $sc->slug, $sc->taxonomy ); echo ‘<li><a href=”‘. $link .'”>’.$sc->name.'</a></li>’; } echo ‘</ul>’; } by … Read more

Order get_terms by term meta

get_terms supports a meta_query which calls a new WP_Meta_Query parameter as you can see here. To query your terms with your desired meta, you could change your function call to something like this: $args = array( ‘taxonomy’ => ‘categoria-de-productos’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘ASC’, ‘hide_empty’ => false, ‘hierarchical’ => false, ‘parent’ => 0, ‘meta_query’ … Read more