Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Total Views column on Admin list posts pages
<?php
/**
* Add Total Views column on Admin list posts pages
*/
add_action( 'admin_init', 'hook_wpp_views_posts_column', 10 );
function hook_wpp_views_posts_column() {
$post_types = get_post_types( array( 'public' => true ) );
if ( empty( $post_types ) )
return;
foreach ( $post_types as $post_type ) {
add_filter( "manage_edit-{$post_type}_columns", 'add_wpp_views_posts_column' ); // Old hooks: seems more compatible with certain custom post type plugin
//add_action( "manage_edit-{$post_type}_posts_custom_column", 'display_wpp_views_posts_column', 10, 2 ); // Old hooks
//add_filter( "manage_{$post_type}_posts_columns", 'add_wpp_views_posts_column' ); // New hooks
add_action( "manage_{$post_type}_posts_custom_column", 'display_wpp_views_posts_column', 10, 2 ); // New hooks
}
}
function add_wpp_views_posts_column($posts_columns) {
$posts_columns['wpp_views'] = __( 'Views', 'site-plugin');
return $posts_columns;
}
function display_wpp_views_posts_column($column_name, $post_id) {
if ('wpp_views' == $column_name) {
echo wpp_get_views( $post_id );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.