Skip to content

Instantly share code, notes, and snippets.

@cabrerahector
Last active April 15, 2023 17:57
Show Gist options
  • Save cabrerahector/138a1926cf8d764a4817e7ff396c9219 to your computer and use it in GitHub Desktop.
Save cabrerahector/138a1926cf8d764a4817e7ff396c9219 to your computer and use it in GitHub Desktop.
[WordPress Popular Posts] Adding Views column to the Posts Manage page

Adding Views column to the Posts Manage page

This Gist shows how to add a sortable Views column to the post/page/custom-post-type edit screen.

Out of the box, the script will display the Views column on both the Posts and Pages screens. You can change that by editing the $post_types_with_views_column array at the beginning of the script.

How do I add this to my site?

  1. Using the text editor of your preference, create a file called wpp-sortable-columns.php and add the contents of this script to it.
  2. Place the wpp-sortable-columns.php file in wp-content\plugins.
  3. If you haven't already, please install the WordPress Popular Posts plugin as it's required for this to work.
  4. Go to Plugins and activate the WordPress Popular Posts Sortable Columns plugin.
  5. Profit!
<?php
/**
* Plugin Name: WordPress Popular Posts Sortable Columns
* Plugin URI: https://wordpress.org/plugins/wordpress-popular-posts/
* Description: Adds sortable Views column to the admin area.
* Version: 1.0.2
* Requires at least: 5.3
* Requires PHP: 7.2
* Author: Hector Cabrera
* Author URI: https://cabrerahector.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: wordpress-popular-posts
* Domain Path: /languages
*/
namespace WordPressPopularPosts;
if ( ! defined('WPINC') ) {
die();
}
/**
* Array of post type(s) we want to sort by views count.
*/
$post_types_with_views_column = array('post', 'page');
/**
* Adds Views column to item list.
*
* @param array $columns
* @return array
*/
function item_list_add_views_column( $columns ) {
$columns['pageviews'] = __('Views', 'wordpress-popular-posts');
return $columns;
}
/**
* Displays the actual views count of each post in the list.
*
* @param array $column
* @param int $post_id
*/
function item_list_views_column($column, $post_id) {
if ( $column == 'pageviews' ){
echo wpp_get_views($post_id);
}
}
/**
* Makes the Views column sortable.
*
* @param array $columns
* @return array
*/
function item_list_sortable_columns($columns) {
$columns['pageviews'] = 'pageviews';
return $columns;
}
/**
* Adds the pageviews field to the query.
*
* @param string $fields
* @param object $wp_query
* @return string
*/
function item_list_query_fields($fields, $wp_query) {
global $pagenow, $wpdb, $post_types_with_views_column;
if (
is_admin()
&& 'edit.php' == $pagenow
&& isset($wp_query->query['post_type'])
&& in_array($wp_query->query['post_type'], $post_types_with_views_column)
) {
$fields .= ", IFNULL(`{$wpdb->prefix}popularpostsdata`.`pageviews`, 0) AS pageviews";
}
return $fields;
}
add_filter('posts_fields', __NAMESPACE__ . '\item_list_query_fields', 10, 2);
/**
* Joins WPP's data table.
*
* @param string $join
* @return string
*/
function item_list_query_join($join) {
global $wp_query, $pagenow, $wpdb, $post_types_with_views_column;
if (
is_admin()
&& 'edit.php' == $pagenow
&& isset($wp_query->query['post_type'])
&& in_array($wp_query->query['post_type'], $post_types_with_views_column)
) {
$join .= "LEFT JOIN `{$wpdb->prefix}popularpostsdata` ON `{$wpdb->posts}`.`ID` = `{$wpdb->prefix}popularpostsdata`.`postid` ";
}
return $join;
}
add_filter('posts_join', __NAMESPACE__ . '\item_list_query_join');
/**
* Sort items by pageviews on demand.
*
* @param string $orderby_statement
* @param object $wp_query
* @return string
*/
function item_list_orderby($orderby_statement, $wp_query) {
global $pagenow, $post_types_with_views_column;
if (
is_admin()
&& 'edit.php' == $pagenow
&& isset($wp_query->query['post_type'])
&& in_array($wp_query->query['post_type'], $post_types_with_views_column)
&& ( isset($wp_query->query['orderby']) && 'pageviews' == $wp_query->query['orderby'] )
) {
$orderby_statement = "pageviews " . ( isset($wp_query->query['order']) && $wp_query->query['order'] == 'asc' ? 'ASC' : 'DESC' );
}
return $orderby_statement;
}
add_filter('posts_orderby', __NAMESPACE__ . '\item_list_orderby', 10, 2);
/**
* Finally, add various hooks so our Views column works!
*/
foreach($post_types_with_views_column as $post_type) {
add_filter('manage_' . $post_type . '_posts_columns' , __NAMESPACE__ . '\item_list_add_views_column');
add_action('manage_' . $post_type . '_posts_custom_column' , __NAMESPACE__ . '\item_list_views_column', 10, 2);
add_filter('manage_edit-' . $post_type . '_sortable_columns', __NAMESPACE__ . '\item_list_sortable_columns');
}
/**
* Adjust the width of the Views column.
*/
function views_column_width() {
echo '<style type="text/css">';
echo '.column-pageviews { overflow: hidden; width: 100px; }';
echo '</style>';
}
add_action('admin_head', __NAMESPACE__ . '\views_column_width');
@X-Raym
Copy link

X-Raym commented Apr 15, 2023

Excellent thx! I was stuck on the sorting part :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment