Skip to content
WJunction - Webmaster Forum

WordPress How to display popular posts by views of each category?

Status
Not open for further replies.

Divvy

Experienced Member
806
2009
18
Hello guys,

Can someone help me with this code?
I want to display most popular posts by views of each category...
I already have a code working but only shows the popular posts of all, not for each category...

Here is my code in single.php file:

PHP:
<?php
foreach(get_the_category(get_the_ID()) as $keys){
    $cu = $keys->cat_name;
}
?>
<h2 class="single-post-external" style="background: #9F0C67; padding: 8px 0 8px 8px;">Most Viewed Posts in <?php echo $cu; ?> Category<?php //echo  "Like ".get_the_title(); ?></h2>    
    <?php setPostViews(get_the_ID()); ?>
    <?php query_posts('posts_per_page=8&meta_key=post_views_count&orderby=meta_value_num&order=DESC'); ?>
    <div id="main_container" class="mrCatouter container  clearfix">
    <?php while ( have_posts() ) : the_post(); ?>

And in my functions.php file I have this:
PHP:
function setPostViews($postID) {    
    $count_key = 'post_views_count';    
    $count = get_post_meta($postID, $count_key, true);    
    if($count==''){        
        $count = 0;        
        delete_post_meta($postID, $count_key);        
        add_post_meta($postID, $count_key, '0');    
    }else{        
        $count++;        
        update_post_meta($postID, $count_key, $count);    
    }
}

I think that I need to change:
Code:
query_posts('posts_per_page=8&meta_key=post_views_count&orderby=meta_value_num&order=DESC');
but I don't have sure what to do...

Hope to see some help here :)

Thank you guys!

Kind regards,
Divvy

__________________
Added after 5 minutes:

I found out that adding a &cat= after DESC and enter the id number for the category it works :)
But I dont want to show a specific category, I want to show the category where the post is in...
Any ideas?
 
Last edited:

3 comments

just add &cat=(Category ID) on your query.

example:
query_posts('posts_per_page=8&meta_key=post_views_count&orderby=meta_value_num&order=DESC&cat=1');
 
Last edited:
Hey kaizer01, thank you for your reply.

Yeah, I know that... but I dont want to show a specific category... I want to show the category where the post is in :)
Any idea how to do that?

__________________
Added after 17 Hours 18 minutes:

Resolved :)
 
Last edited:
Status
Not open for further replies.

About the author

D
Experienced Member · Joined
806
Messages
18
Reactions
18
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom