get_term_children for immediate children only (not grandchildren)

Use the get_terms() function instead: $term_children = get_terms( ‘mytaxname’, array( ‘parent’ => get_queried_object_id(), ) ); if ( ! is_wp_error( $terms ) ) { foreach ( $term_children as $child ) { echo ‘ <div class=”product-archive”> <div class=”post-title”> <h3 class=”product-name”><a href=”‘ . get_term_link( $child ) . ‘”>’ . $child->name . ‘</a></h3> </div> </div> ‘; } } get_terms() … Read more

Show posts without term

You cannot use the same object of WP_Query twice. Therefore you need to create another one with a tax_query parameter to fetch posts which are not assigned to any term. //fetch all reviews which have no assigned term in ‘review-product’ $taxonomy = ‘review-product’; $post_type=”reviews”; $args = [ ‘post_type’ => $post_type, ‘tax_query’ => [ [ ‘taxonomy’ … 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

Order get_terms using a Custom Field

Rather than outputting your terms in that initial loop, I would use it instead to build a new array, with your issue_date as the key: $my_new_array = array( ); foreach ( $terms as $term ) { $issue_date = get_field( ‘issue_date’, $term ); $my_new_array[$issue_date] = $term->name; } You can then loop through this new array 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

tax_query in get_posts() not working?

tax_query takes an array of tax query arguments arrays (it takes an array of arrays) but you are using only single array. The correct code is as following. $uposts = get_posts( array( ‘post_type’ => ‘product’, ‘numberposts’ => -1, ‘tax_query’ => array( array( ‘taxonomy’ => $cat->taxonomy, ‘field’ => ‘slug’, ‘terms’ => array($cat->slug), ‘operator’ => ‘IN’, ) … Read more

wp set object terms vs wp set post terms

Right there is no big difference between them, actually wp_set_post_terms() uses wp_set_object_terms() but does few extra check for you. That’s also noted on wp_set_object_terms() Codex page: Perhaps the wp_set_post_terms() is a more useful function, since it checks the values​​, converting taxonomies separated by commas and validating hierarchical terms in integers. http://codex.wordpress.org/Function_Reference/wp_set_object_terms#Notes

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