• I’m making a post count at dashboard WordPress but it not is displaying.

    I founded this tutorial:

    This code display the count view for single.php

    In my functions:

    `function wpmidia_set_post_views($postID) {

    $cookie = strtotime(date(‘Y-m-d’));
    $pv_url = ‘wpmidia_’.md5($_SERVER[‘REQUEST_URI’]);

    if( is_single() && !isset($_COOKIE[$pv_url]) ){

    $count_key = ‘_wpmidia_post_views_count’;
    $count = get_post_meta($postID, $count_key, true);
    if($count==”){
    $count = 1;
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, $count);

    setcookie($pv_url, $cookie, time()+3600, COOKIEPATH, COOKIE_DOMAIN, false); // 1h

    }else{
    $count++;
    update_post_meta($postID, $count_key, $count);
    }

    }
    }`

    In my single.php:

    <?php echo wpmidia_get_post_views($post->ID); ?>

    How I said this code display the count post in WordPress pages single, page or another.

    This code show the count view at Dashboard. Exactly what I want to do. The “Views” field in the dashboard is created, however the counter does not work, nothing is displayed:

    `add_filter(‘manage_posts_columns’, ‘wpmidia_posts_column_views’);
    function wpmidia_posts_column_views($defaults){
    $defaults[‘post_views’] = __(‘Views’);
    return $defaults;
    }

    add_action(‘manage_posts_custom_column’, ‘wpmidia_custom_column_views’,5,2);
    function wpmidia_custom_column_views($column_name, $id){
    if($column_name === ‘post_views’){
    $count = get_post_meta($id, ‘_wpmidia_post_views_count’, true);
    if( !empty($count) ){
    echo $count. ‘ views’;
    }
    }
    }`

    How can I solve this?

    • This topic was modified 7 years, 3 months ago by santosluiz.
    • This topic was modified 7 years, 3 months ago by santosluiz.
    • This topic was modified 7 years, 3 months ago by santosluiz.
    • This topic was modified 7 years, 3 months ago by santosluiz.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Everything seems to be in order, so it’s going to be a matter of basic debugging. Do you have WP_DEBUG defined as true in wp-config.php? You should. Some easy to miss error might be caught by an innocuous notice or warning.

    Does the count data exist in the DB? Use phpMyAdmin to confirm. If it doesn’t, focus debugging on the counting function, otherwise focus on the column output function. Echo out or error_log various variable values to ensure they contain what they should. Add text output at strategic locations in code to ensure the program flow goes where it should. For example, add an else condition to if( !empty($count) ) to output “No views”. Now if you see nothing, there is a problem with the initial conditional or adding the action itself.

    Add an error_log() call as the first line of the function. If you get no logged error, your callback was never called. Otherwise there’s an issue with the column name passed. Keep installing checks that narrow down source of the problem until you can determine exactly what and where it is. Follow a similar process to debug the counting function if there is no count data in the DB.

    When developing, it’s best to work on a installation that has no plugins and runs one of the default twenty* themes. Plugins and elaborate themes can introduce hard to find conflicts. If that’s not possible, at least temporarily deactivate all plugins and switch to a twenty* theme to rule out such conflicts.

    Good luck!

Viewing 1 replies (of 1 total)
  • The topic ‘Post count view at dashboard WordPress not displaying’ is closed to new replies.