DEV Community

Cover image for WordPress post view count
Keramot UL Islam
Keramot UL Islam

Posted on

5 1

WordPress post view count

There are lots of articles over the web regarding "WordPress post view count" and "Creating a custom column for Post table". But unfortunately, most of them are not bug-free. In this article, I'm going to cover both of them.

At first, let's see how to create post view count and stored as metadata.

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );

function wp_set_post_views( $postID ) {
    $count_key = 'wpb_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 );
    }
}

function wp_get_post_views() {
    if ( is_single() ) {
        wp_set_post_views(get_the_ID());
    }
}
add_filter( 'the_content', 'wp_get_post_views' );
Enter fullscreen mode Exit fullscreen mode

Now let's create a custom column for Post table and show post view counts.

function wp_posts_columns( $defaults ) {
    $defaults['wpb_post_views_count']  = esc_html__( 'Post Views', 'text_domain' );
    return $defaults;
}
add_filter( 'manage_post_posts_columns', 'wp_posts_columns' );


function wp_post_custom_column( $column_name ) {
    if ( $column_name == 'wpb_post_views_count' ) {
        $count = get_post_meta( get_the_ID(), 'wpb_post_views_count', true );
        $view_count = $count == '0' ? '1' : $count;
        echo esc_html__( $view_count, 'text_domain' );
    }
}
add_action( 'manage_post_posts_custom_column', 'wp_post_custom_column', 10, 2 );


function wp_post_column_sortable() {
    $columns['wpb_post_views_count'] = ['wpb_post_views_count', true ];
    return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'wp_post_column_sortable' );


function wp_sortable_custom_column_query( $query ) {
    $orderby = $query->get( 'orderby' );

    if ( 'wpb_post_views_count' == $orderby ) {

        $meta_query = array(
            array(
                'key' => 'wpb_post_views_count',
                'value'   => '',
                'compare' => '>',
            ),
        );

        $query->set( 'meta_query', $meta_query );
        $query->set( 'orderby', 'meta_value' );
    }
}
add_action( 'pre_get_posts', 'wp_sortable_custom_column_query' );
Enter fullscreen mode Exit fullscreen mode

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay