dsebao
6/12/2013 - 3:13 AM

Simple visit counting in WordPress

Simple visit counting in WordPress

<?php
//IN single.php put this
?>


<?php set_post_views(get_the_ID());?>
<!-- to show the most readed post of the week (showing 5 post)-->
<ul class="more-readed-widget">
  <?php function filter_where($where = '') {
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'"; 
    return $where;
    $i = 0;
    }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string . 'meta_key=post_views_count&orderby=meta_value_num&order=DESC&posts_per_page=5');
    while (have_posts()): the_post(); 
    $i++;
    ?>
    <li><a href="<?php the_permalink()?>"><?php the_title()?></a></li>
    <?php endwhile; wp_reset_query();?>
</ul>
<?php
//In function.php

function get_post_views($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";
    }
    return ' ('.$count.')';
}

function set_post_views($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);
    }
}

?>