Help alphabetically sorting $terms from get terms(‘wpsc_product_category’

This code works! Sorts by category name (visible name), produces a list of links of categories (that aren’t empty) that are children of category_id. <?php //display sorted list of wpsc product categories $category_id = 10; $terms = get_terms(‘wpsc_product_category’,’hide_empty=1&parent=”.$category_id); usort($terms, function($a, $b) { return strcmp($a->name, $b->name); }); if ($terms) { foreach($terms as $term) { ?> <div> … Read more

Add current class to queried term on taxonomy term archive

Try this instead: $terms = get_terms(‘MYTAX’); $currentterm = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) ); foreach ($terms as $term) { $class = $currentterm->slug == $term->slug ? ‘live’ : ” ; echo ‘<li class=”‘. $class .'”><a href=”http://website.com/?MYTAX=’. $term->slug .'”>’ . $term->name . ‘</a></li>’; } Basically you just need to reset your $class variable back … Read more

List subcategory on taxonomy term page

You can make use of get_term_children() instead of get_categories() You can try something like this $term = get_queried_object(); $term_id = $term->term_id; $taxonomy_name = $term->taxonomy; $termchildren = get_term_children( $term_id, $taxonomy_name ); echo ‘<ul>’; foreach ( $termchildren as $child ) { $term = get_term_by( ‘id’, $child, $taxonomy_name ); echo ‘<li><a href=”‘ . get_term_link( $term, $taxonomy_name ) . … Read more

How to preserve edits to Name or Slug of term when using wp_update_term on save?

So, thanks to cjbj I finally found the correct answer! I needed to use edited_term instead of edit_term. Very subtle difference. edited_term fires after a term has been saved. // insert stuff into description field of taxonomy function insert_taxonomy_content( $term_id, $tt_id, $taxonomy ){ // only insert content on certain taxonomies if ( $taxonomy === ‘some_custom_taxonomy’ … Read more

Get taxonomy terms only of the WP_Query current posts

You’d probably do something like this $args = array( ‘post_type’ => ‘product’, ‘product_cat’ => get_queried_object() ); $query = new WP_Query( $args ); $termsInPosts = array(); if ( $query->have_posts() ) { while( $query->have_posts() ) : $query->the_post(); $postTerms = wp_get_post_terms(get_the_ID(), ‘brands’, array(‘fields’ => ‘term_id’)); foreach ($postTerms as $postTerm) if (!in_array($postTerm->term_id, $termsInPosts)) $termsInPosts[] = $postTerm->term_id; endwhile; } $marcas_terms … Read more

Get Objects While Deleting term

Use the pre_delete_term hook, which fires before the actual deleting occurs, so the relationships will still be present. function wpse_296972_pre_delete_term( $term_id, $taxonomy_name ) { if ( $taxonomy_name === ‘organization’ ) { $objects = get_objects_in_term( $term_id, $taxonomy ); // Do something with $objects. } } add_action( ‘pre_delete_term’, ‘wpse_296972_pre_delete_term’ );

Pass array of taxonomy terms to wp_query

The problem is ambiguity, you can’t just put in ‘relation’ => ‘OR’ in the main parameter list, because how would WP_Query know if it’s for the tax_query and not the meta_query? For how it’s supposed to work, and where the relation parameter goes we need to refer to the official docs for WP_Query which gives … Read more