database - Post ViewsHit Counter Problem?

I have decide to use WordPress as a CMS to my 5 moths old website. I have already added all previous posts to my new sit

I have decide to use WordPress as a CMS to my 5 moths old website. I have already added all previous posts to my new site ( WordPress ). But now I need to change view counts to previous views counts. I can't find hit_count or views_count from PhpMyAdmin database. How to change views count number manually on WordPress? Please help.

function.php

<?php
// function to display number of posts.
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 to 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);
    }
}

// Add it to a column in WP-Admin - (Optional)
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
function posts_column_views($defaults){
    $defaults['post_views'] = __('Views');
    return $defaults;
}
function posts_custom_column_views($column_name, $id){
 if($column_name === 'post_views'){
        echo getPostViews(get_the_ID());
    }
}
?>

single.php

<?php setPostViews(get_the_ID()); ?>

I have decide to use WordPress as a CMS to my 5 moths old website. I have already added all previous posts to my new site ( WordPress ). But now I need to change view counts to previous views counts. I can't find hit_count or views_count from PhpMyAdmin database. How to change views count number manually on WordPress? Please help.

function.php

<?php
// function to display number of posts.
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 to 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);
    }
}

// Add it to a column in WP-Admin - (Optional)
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
function posts_column_views($defaults){
    $defaults['post_views'] = __('Views');
    return $defaults;
}
function posts_custom_column_views($column_name, $id){
 if($column_name === 'post_views'){
        echo getPostViews(get_the_ID());
    }
}
?>

single.php

<?php setPostViews(get_the_ID()); ?>
Share Improve this question asked Jun 29, 2015 at 9:05 NilohnNilohn 51 silver badge4 bronze badges 3
  • your setPostViews(get_the_ID()); function will update your view by 1 each time it runs. For you to set a views manually you need to create a new function to do so. Your existing functions don't have that feature. – Karun Commented Jun 29, 2015 at 9:19
  • please can I have the function code? I'm new to WordPress. – Nilohn Commented Jun 29, 2015 at 9:26
  • Check the answer below. – Karun Commented Jun 29, 2015 at 9:29
Add a comment  | 

1 Answer 1

Reset to default 2

Use the following functionto set views manually

function setPostViewsManually($postID, $viewCount) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    update_post_meta($postID, $count_key, $viewCount);
}

You can run this code by

<?php setPostViewsManually(get_the_ID(), 1234); ?>

The above code will set the view to '1234' for the current post. Run this function carefully else your new views will always be '1234' because this function will keep running every time.

Just use it once and remove the code.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745148895a4613774.html

相关推荐

  • database - Post ViewsHit Counter Problem?

    I have decide to use WordPress as a CMS to my 5 moths old website. I have already added all previous posts to my new sit

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信