wordpress snippets last updated   : April 22, 2014
we now have 631 snippets

Display post views within admin post columns

wordpress snippet

I posted a cool snippet yesterday to (track post views using post meta) I wanted to add a little to that today. Adding this snippet to the functions.php of your wordpress theme will display the post views next to each post in the wordpress admin.

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

  • http://www.facebook.com/konstantin.yelin Konstantin Yelin

    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?

    • http://wpsnipp.com Kevin Chard

      @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.

      • http://www.facebook.com/konstantin.yelin Konstantin Yelin

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

        • http://wpsnipp.com Kevin Chard

          Cool, glad to see you got things working.

  • Xwtiko

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

  • Anonymous

    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.

  • http://twitter.com/shakirul007 Shakil Ahmed

    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

  • Nizam Kazi

    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!)

  • Ahmed

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

jqsnipp.com | we just launched jquery snippets website