Ana DEV
Ana DEV

Reputation: 1030

How to order wordpress taxonomy categories depending on their posts view activity(view count)

I displayed the wordpress taxonomy categories with the following code

                            $video_args = array(                                        
                            'hide_empty' => true,                                       
                            'fields' => 'all',                                      
                            'hierarchical' => true,                     
                            'orderby' => 'term_order',                                      
                            'child_of' => 0,                                        
                            'get' => '',                                        
                            'name__like' => '',                                     
                            'pad_counts' => false,                                      
                            'taxonomy' => 'video-category',                                     
                            'cache_domain' => 'core'                                
                            );      
                        $v_terms = get_terms('video-category', $video_args);

Now I wanted to order categories depending on their inner posts view.For example if in the category Music posts will be viewed mostly it should become the first and etc.How can I do this?

Upvotes: 1

Views: 267

Answers (1)

vard
vard

Reputation: 4136

I came with a different solution that I first thought with a custom query that will get the SUM of posts views for each term. That will make one query per category which is not too much greedy if you don't have too much categories.

First we get your categories (it's your original code, just removed the orderby as it is not relevant here):

$video_args = array(                                        
    'hide_empty' => true,                                       
    'fields' => 'all',                                      
    'hierarchical' => true,                                                     
    'child_of' => 0,                                        
    'get' => '',                                        
    'name__like' => '',                                     
    'pad_counts' => false,                                      
    'taxonomy' => 'video-category',                                     
    'cache_domain' => 'core'                                
    );      
$v_terms = get_terms('video-category', $video_args);

Then we query the total views count for each category - please note that this is an untested code but should work:

// Query the total post count for each category
global $wpdb;
foreach($v_terms as $key => $term) {
    $count_views = $wpdb->get_results($wpdb->prepare("
        SELECT SUM(pm.meta_value) AS view_count FROM {$wpdb->postmeta} pm
        LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
        LEFT JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id
        LEFT JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
        WHERE pm.meta_key = 'post_views_count'
        AND tt.taxonomy = 'video-category'
        AND tt.term_id = %d
    ", $term->term_id));

    // Add to the category object the result
    $v_terms[$key]->count_views = (!empty($count_views)) ? $count_views[0]->view_count : 0;
}

And finally sort the category array by views count:

function cmp($a, $b) {
    if ($a->count_views == $b->count_views) {
        return 0;
    }
    return ($a->count_views > $b->count_views) ? -1 : 1;
}
usort($v_terms, "cmp");

Upvotes: 1

Related Questions