1

I have a taxonomy list. and taxonomy have two custom post types. I want to order taxonomy according to this result. Lets say :

A = count of my A custom post type 
B = count my B custom post type 

 (A + B) = AB
(A * 100% ):AB = RESULT

Example:
lets say taxonomy have 37 A posts types  and 70 B  posts types.
so (37 A + 70 B) = 107 AB
(37 * 100 ) : 107 = 34.57943925233645 %
it means  34.57943925233645 percent of  count of  total posts of taxonomy belongs to A post type.

i want to order taxonomy list according to this result.. is It possible? At the present i use this code to order taxonomies. This code show taxonomy list and order by count of A post type of taxonomy.

<?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 );
}

$i                 = 1;
// get the terms you need
$terms        = get_terms( 'MY_CUSTOM_TAXONOMY', 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_A_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 $b['count'] - $a['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="%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 ?>
  • You probably want to do as much of this in the database as possible, e.g. the database can return you a list of counts per taxonomy in one go rather than fetching all posts in each taxonomy one by one and then counting them on the server side. It might be best to write this in SQL first then worry about making it WordPress afterwards. – Rup Jun 27 at 10:27
  • is it possible to do it by modifying this code without sql – Ramin Mahmudov Jun 27 at 10:43
  • You could extend this code to also get the B counts and compute the percentages and then order by that - that's fairly straightforward isn't it? But this does a lot of database access so you probably want to cache the results with the transients API if you have to do it like this so you at least don't have to count all of the posts on every request. – Rup Jun 27 at 10:47
  • I think it is not right way to do it with post_type => array ( 'A' , 'B'); need apsolutely different algorithim – Ramin Mahmudov Jun 27 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.