Question with get_the_term_list

You could use get_terms() and loop through it – using the term description as the “short name” and slug as the specific class ( or you could use an array of colors of your choice ). Let’s look at a quick example: <?php $terms = wp_get_post_terms( $post_id, ‘category’ ); if( ! empty( $terms ) ) … Read more

List child terms if parent but show nothing on children

For reliability, you can get the current term being viewed from the queried object from $GLOBALS[‘wp_the_query’] $current_term = sanitize_term( $GLOBALS[‘wp_the_query’]->get_queried_object() ); To answer your question, if Southern Africa is top level, it should have a parent term ID of 0. All child terms should have a numeric value as parent which corresponds to the parent … Read more

get_the_terms error

I ended up using the wp_get_object_terms() function instead of get_the_terms(). The get_the_terms() function attempts to cache the terms where wp_get_object_terms() does not. The fixed code to get the hero images CPT’s ID is: $hero_image_id = ( $assigned_hero_images = wp_get_object_terms( $post_parent, ‘hero_images’ ) ) && ( $hero_image = array_shift( $assigned_hero_images ) ) && isset( $hero_image->term_id ) … Read more

AJAX response, edit tags

Wait for WordPress 4.7 on 6th December. It has this almost built-in. If I got it right, then you’ll want to prevent some terms from deletion. I already made a snippet for that which works with WP 4.7 add_filter( ‘user_has_cap’, function ( $allcaps, $caps, $args ) { if ( ! isset( $args[0] ) || ‘delete_term’ … Read more

Limit number of terms that a custom taxonomy can save per custom post type

Taxonomy Single Term is a small library that helps to implemement this functionality. Usage Include the class.taxonomy-single-term.php file from within your plugin or theme Initialize the class (update the taxonomy slug with your own): $custom_tax_mb = new Taxonomy_Single_Term( ‘custom-tax-slug’ ); Optional The second parameter is an array of post_types and the third parameter is either … Read more

Does get_terms() use any sort of caching on its query?

User gmazzap has a good explanation of Object Cache you may want to read over. If we take a look at get_terms() we can see it creates a new WP_Term_Query() and that classes get_terms() is where the potential caching happens. Line 666 specifically. Just a little further down we can see if it test if … Read more