Support » Plugins » Display Most Popular Posts without plugin – (by Viewed & Taxonomy)

  • I used this code and it worked fine for posts…

    How do I use with taxonomy?

    http://www.be-studios.com/blog/2011/11/30/wordpress-display-most-popular-viewed-posts-without-a-plugin

    Add this on functions.php:

    function getPostViews($postID){
        $count_key = 'post_views_count';
        $count = get_post_meta($postID, $count_key, true);
        if($count==''){
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
            return "0 View";
        }
        return $count.' Views';
    }
    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);
        }
    }

    The add this when the loop starts within single.php:

    setPostViews(get_the_ID());

    Finally, query the posts by this meta-value, por example in the sidebar.php:

    <?php
    	query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC');
    	if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    	<?php
    	endwhile; endif;
    	wp_reset_query();
    ?>

    How do I use with taxonomy?

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display Most Popular Posts without plugin – (by Viewed & Taxonomy)’ is closed to new replies.