Child terms from multiple parents?

We can filter the generated SQL query through the terms_clauses filter before the SQL query is executed. What we will be doing is to introduce a new parameter called wpse_parents which will accept an array of parent term ID’s to get children from. Note, this new parameter works exactly the same as the build-in parameter, … Read more

wp_list_categories: order by term order?

The wp_list_categories() function calls the get_categories() function, that’s a wrapper for the get_terms() function, that creates an instance of the WP_Term_Query class. It doesn’t look like it supports ordering by term order. If the plugin uses the term_order column in the wp_terms table, then you can try to add a support for it’s ordering, via … Read more

Get multiple term IDs by slug, and then exclude them in get_terms

You can get terms, from multiple slugs, with the slug argument: $exclude = get_terms ( [ ‘slug’ => [ ‘president’, ‘vice-president’, ‘admin’, ‘rnd’ ], ‘taxonomy’ => $cat_type, ‘fields’ => ‘ids’, ] ); where we use the fields argument to return only term ids.

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)