X

How to Display Post Views Within Admin Post Columns

Snippets by IsItWP

Are you looking for a way to dislay the post views next to each post in the WordPress admin? While there’s probably a plugin for this, we have created a quick code snippet that you can use to display post views within admin post columns in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 21 best WordPress real estate themes and how to start a blog.

Comments  Leave a Reply

  1. How to add desc/asc to the column?

  2. Interesting. This is a great idea…but it no longer works as intended. When I added it, the Posts list only showed my most recent post. Nothing “broke”, as all posts were live and kept working…it’s just the list that got truncated. I’m loving this site, by the way!

    1. do you also have this snippet running on your site as well ?

      http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/

      they work together, the one at this link collects the views and the snippet on this page displays the meta data within the admin when you view posts section.

      1. TA-DAAAAAAAAAAAAAA

        Awesome.

  3. Another option I wish you add
    Sortable Views Column
    It hepls the show the popular posts

  4. I can see Views column at my Dashboard, but view count seems not updating. Do I need different IP? Will it count my IP for at-least once(which it is not doing right now!)

  5. I can’t see view count . In the view count section , I saw this error message

    Fatal error: Call to undefined function echo_views() inE:Softwarexampphtdocswpwp-contentthemesarclitefunctions.php on line 10

  6. Its working. Thanks
    But sorting the posts based on views would have made it complete. It helps to pull up posts that need more attention to generate traffic.

  7. Nice snippet. Is there a way to sort the posts by the number of views? It would be very helpful!

  8. Konstantin Yelin June 18, 2011 at 7:44 am

    Tried the snippet on two themes (multisite) – froze the admin pages in both cases and showing only 2 posts for each site. Why would that happen?

    1. @facebook-36916051:disqus this should not happen at all I would guess that the snippet may have issues with a pluggin you already have running.  I have never tried this snippet on multi-site version of wordpress so I’m not sure. However it should not be a problem,

      I assume you have this snippet setup
      http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/

      Then you can use the above to display the post view in a new column but both snippets are required. If you disable all the pluggins you can see if it works then and then enable one by one to see what plugin is a problem.

      1. Konstantin Yelin June 27, 2011 at 9:47 am

         thank you, Kevin, got it! I did not realize both snippets must be setup for everything to work

        1. Cool, glad to see you got things working.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!