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

get_term_link() returns incorrect url

WordPress allows for customization of the default URL rewrite behavior (/slug/term) with a rewrite arguments array passed to the register_taxonomy() function. By defining values for the slug or hierarchical keys in this array, the default URL structure can be altered to suit your needs. It may also be altered such that unexpected results are returned. … Read more