Taxonomy list. Order by a specific custom post type count

by Ramin Mahmudov   Last Updated June 14, 2019 13:08 PM

I have a custom taxonomy and two custom post types (A and B post types). I created a taxonmy list and this list also display B posts count of taxonmoy. Taxonomy list is ordered by post count. My problem is that ordering of taxonomy is going according to total post count ( total count of both A and B post types ) of taxonomy. I want ordering to be according to B post type count. Please need help this my code

do_action('wcw_style','wcw_style_func');
        /** return category list */
        if($instance['wcw_taxonomy_type']){
            $va_category_HTML ='<div class="ve-cat-widget-div">';
            $va_category_HTML .='<ul class="ve-cat-widget-listing">';
                $args_val = array( 'hide_empty=0' );    
                $i=1;
                $excludeCat= $instance['wcw_selected_categories'] ? $instance['wcw_selected_categories'] : '';
                $wcw_action_on_cat= $instance['wcw_action_on_cat'] ? $instance['wcw_action_on_cat'] : '';
                if($excludeCat && $wcw_action_on_cat!='')
                $args_val[$wcw_action_on_cat] = $excludeCat;
                function wpse340250_term_count( WP_Term $term, $post_type) {
    $q_args = [
        'post_type' => $post_type,
        'nopaging' => true, // no limit, pagination
        'fields' => 'ids', // only return post id's instead of full WP_Post objects will speed up
        'tax_query' => array(
            array(
                'taxonomy' => $term->taxonomy,
                'field'    => 'term_id',
                'terms'    => $term->term_id,
            ),
        ),
    ];
    $term_count = get_posts($q_args);

    return count($term_count);
}

                $terms = get_terms( $instance['wcw_taxonomy_type'], array('orderby' => 'count', 'order' => 'DESC','hide_empty'=>0 ));
                if ( $terms ) { 

                    foreach ( $terms as $term ) {

                        $term_link = get_term_link( $term );

                        if ( is_wp_error( $term_link ) ) {
                        continue;
                        }

                    $carrentActiveClass=''; 

                    if($term->taxonomy=='category' && is_category())
                    {
                      $thisCat = get_category(get_query_var('cat'),false);
                      if($thisCat->term_id == $term->term_id)
                        $carrentActiveClass='class="active-cat"';
                    }

                    if(is_tax())
                    {
                        $currentTermType = get_query_var( 'taxonomy' );
                        $cat_icon = get_field('logo', $queried_object);
                        $termId= get_queried_object()->term_id;
                         if(is_tax($currentTermType) && $termId==$term->term_id)
                          $carrentActiveClass='class="active-cat"';


                    }   
                        $icon = get_field('logo', $term->taxonomy . '_' . $term->term_id); 
                        $va_category_HTML .= '<li class="logolar" '.$carrentActiveClass.'>' .'<a class="rownum">' .$i++. '</a>'. '</a>';
                        $va_category_HTML .= sprintf('<img src="%s" />', $icon) . '</a>';
                        $va_category_HTML .='<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';
                        if (empty( $instance['wcw_hide_count'] )) {
                        $va_category_HTML .='<span class="post-count">'.wpse340250_term_count($term, 'my_B_custom_post_type').'</span>';
                        }

                        $va_category_HTML .='</li>';
                    }
                }
            $va_category_HTML .='</ul></div>';
            echo $va_category_HTML;
            }   


Related Questions




show posts using wp_list_categories

Updated May 30, 2019 10:08 AM

how taxonomy list custom post count

Updated June 12, 2019 08:08 AM