Support » Plugin: Beautiful taxonomy filters » Only show terms used by Post Type

  • Resolved valuebg

    (@valuebg)


    I saw this post which was not resolved: https://wordpress.org/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)
  • Plugin Author Jonathandejong

    (@jonathandejong)

    Hi @valuebg

    Glad you found yourself a workaround!

    I think you’re being a bit harsh tho considering I offer you to use this plugin Open source, 100% free, no ads while I commit my free time to making it better and working 🙂

    There’s also a github repo where I’m glad to accept PRs for improvements if you want to contribute yourself: https://github.com/beautiful-taxonomy-filters/beautiful-taxonomy-filters
    As long as it’s a solution that wont introduce bugs nor compromise the current solutions speed by making heavier DB calls.

    Best of luck with your project

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.