Only show terms used by Post Type
-
I saw this post which was not resolved: https://www.ads-software.com/support/topic/hide-empty-terms-didnt-work-for-custom-taxonomy-in-multiple-custom-post-type
I don’t think it’s reasonable to suggest using taxonomies exclusively for a single post type…
My workaround solution is to include only terms assigned to published posts of a single type, via
beautiful_filters_dropdown_categories
filter.In my case I used post type ‘product’ and taxonomy ‘mm’
function get_terms_id_by_post_type( $taxonomies, $post_types ) { global $wpdb; $query = $wpdb->get_col( "SELECT t.term_id from $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id WHERE p.post_status = 'publish' AND p.post_type IN('" . join( "', '", $post_types ) . "') AND tt.taxonomy IN('" . join( "', '", $taxonomies ) . "') GROUP BY t.term_id"); return $query; } function modify_categories_dropdown( $args, $taxonomy ) { $types = array('product'); $tax = 'mm'; if ( $taxonomy == $tax ) { $terms = get_terms_id_by_post_type( array($tax), $types ); $term_list = ! empty($terms) ? implode(',', $terms) : false; if ( $term_list ) { $args['include'] = $term_list; } } return $args; } add_filter( 'beautiful_filters_dropdown_categories', 'modify_categories_dropdown', 10, 2 );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Only show terms used by Post Type’ is closed to new replies.