Have a Custom Post Type index page display taxonomy items instead of posts

If you’d like to list the individual courses, i.e. the taxonomy terms, you’d use neither WP_Query nor the WP standard Loop. Instead, make use of the get_terms function to retrieve the courses. It returns an array of term objects (if the taxonomy does exist and has terms matching the function arguments). Iterate over that and … Read more

get_term_children returns WP_Error for custom taxonomy

Since get_term_link() will return a WP_Error object if the term does not exist, you could try: $termchildren = get_term_children( $taxID, $taxType ); echo ‘<ul>’; foreach ( $termchildren as $child ) { $term = get_term_by( ‘id’, $child, $taxType ); $term_link = get_term_link( $term->name, $taxType ); if( ! is_wp_error( $term_link ) ) echo ‘<li><a href=”‘ . $term_link … Read more

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