wp_insert_term is adding a term that has no name

As noted in the OP’s comments, some non-ASCII characters may not be supported in term names under certain conditions. To replace all suspect characters with an underscore, use the following code: $categoryname = preg_replace(‘/[^a-z0-9]/i’, ‘_’, strtolower($categoryname)); wp_insert_term($categoryname, “department”); To replace all suspect characters with a dash, use the following code: $categoryname = preg_replace(‘/[^a-z0-9]/i’, ‘-‘, strtolower($categoryname)); … Read more

How to get term link that crosses different custom post types?

This code display all posts of all categories of genre taxonomy for custom post type book. Now, For different custom post type (author, product ) you have to change Custom Post Type Name inside $arg of WP_Query(). You will get term link using get_term_link($catterm) function or you can use also get_the_term_list(). $args = array( ‘number’ … Read more

Custom Taxonomies Terms as Post Title for Custom Post Types upon Publishing

I’ve pieced together a solution. Let me know if it’s what you need: add_filter(‘the_title’,’term_filter’,10,2); function term_filter($title, $post) { $post = get_post($post) ; if($post->post_type === ‘special_post_type’) { $terms = wp_get_object_terms($post->ID, ‘taxonomy’); if(!is_wp_error($terms) && !empty($terms)) { $title = $terms[0]->name; } } return $title; } Basically, I’m using a filter for the title that checks what post type … Read more

Return only the custom sub-term for custom post type, do not echo term-parent

Here’s more of a complete guide based on the $wp_query object: The Taxonomy First you might want to know in which taxonomy you are, what its name is and retrieve all its available data from the object. // Taxonomy name $taxonomy = get_query_var( ‘taxonomy’ ); // Taxonomy object get_taxonomy( $taxonomy ); // Taxonomy name get_taxonomy( … Read more

same taxonomy for several post types: how to hide empty in a specific post type?

Thanks to this answer, I was able to see the light. Here is how to query taxonomy terms related to (woocommerce) products that are “in stock”. It uses both tax_query (to relate product to taxonomy) and meta_query (to filter out out-of-stock products). $artists = get_terms_by_post_type( array(‘artist’), array(‘product’)); if( !empty($artists) && !is_wp_error( $artists ) ){ // … Read more

Exclude Child Terms From Parent Posts

In your get_terms() call, try setting the hierarchical option to false: $children_terms = get_terms($taxonomy, array( ‘parent’ => get_term_by(‘slug’, $parent_term, $taxonomy)->term_id, ‘hierarchical’ => false )); This option normally defaults to true, which is probably why you’re getting the extra copies.