Taxonomy list. Order by a specific custom post type count

This question is a follow up of this question you asked earlier.
Here we handled getting count per posttype.

So you have all data, just not sorted correctly.
Looking at your code you have one big problem.
Getting and formatting data is done at the same time as you are outputting the data.
For maintainability (and readability) it is best to separate these as much as possible.

Your code did have unused variables. I removed stuff. So check the validity yourself.
The core of the answer (sorting) should work. Do read the comments in the code to get a idea what it does and where what goes.

<?php

// functions probably should be kept in a different file like `functions.php`
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 );
}

// all raw data and other $instance stuff, no html variables
// just gather data.
$excludeCat        = $instance['wcw_selected_categories'] ? $instance['wcw_selected_categories'] : '';
$wcw_action_on_cat = $instance['wcw_action_on_cat'] ? $instance['wcw_action_on_cat'] : '';
$i                 = 1;

// get the terms you need
$terms        = get_terms( $instance['wcw_taxonomy_type'], array( 'orderby' => 'count', 'order' => 'DESC', 'hide_empty' => 0 ) );
$sorted_terms = [];
if ( $terms ) {
    foreach ( $terms as $term ) {
        $sorted_term = [
            'WP_Term'            => $term, // the global term
            'icon'               => get_field( 'logo', $term->taxonomy . '_' . $term->term_id ),
            'carrentActiveClass' => '',
            'count'              => (int) wpse340250_term_count( $term, 'my_B_custom_post_type' ),
            // everything you will need later here
        ];

        // here handle all dynamic checks add them to $sorted_term
        if ( $term->taxonomy == 'category' && is_category() ) {
            $thisCat = get_category( get_query_var( 'cat' ), false );
            if ( $thisCat->term_id == $term->term_id ) {
                $sorted_term['carrentActiveClass'] = 'class="active-cat"';
            }
        }

        // for the sake of getting to the core of the answer I left stuff out.


        // add the complete sorted_term to the collection
        $sorted_terms[] = $sorted_term;
    }

    // sort an array based on a sub value  https://stackoverflow.com/a/2477524/933065
    usort( $sorted_terms, function ( $a, $b ) {
        return $a['count'] - $b['count'];
    } );

}
// all done gathering data now we handle html output
?>
<?php if ( ! empty( $sorted_terms ) ) : ?>
    <div class="ve-cat-widget-div">
        <ul class="ve-cat-widget-listing">
            <?php foreach ( $sorted_terms as $sorted_term ) : ?>
                <li class="logolar" <?php echo $sorted_term['carrentActiveClass']; ?>>
                    <a class="rownum"><?php echo $i ++ ?></a>
                    <?php printf( '<img src="https://wordpress.stackexchange.com/questions/340496/%s" />', $sorted_term['icon'] ) ?>
                    <a href="<?php echo esc_url( get_term_link( $sorted_term['WP_Term'] ) ) ?>"><?php echo $sorted_term['WP_Term']->name ?></a>
                    <?php if ( empty( $instance['wcw_hide_count'] ) ) : ?>
                        <span class="post-count"><?php echo $sorted_term['count'] ?></span>
                    <?php endif ?>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif ?>

Leave a Comment

tech