How to sort the "Views column" in the WordPress Admin Dashboard?

Posted in: Newspaper
Post count: 2

Hi, can you please send me the part of code which I need to put to the “td_page_views.php” file so that I can sort the “Views column” in the Wordpess Admin Backend/Dashboard. Thx.

Post count: 23312

Hello skajic,

Unfortunately, I do not have any such a simple code for this, sorry! Please notice that this is not a simple task and if you want to do this, you should make some additional customization through code that involves custom work and notice that we cannot provide this type of services at the moment, sorry! If you want to display more levels in our theme and if you are not aware of the steps which are needed to take in this regard, please consider hiring a freelancer/developer to help you in this regard! Usually, we recommend the developers from studio.envato.com.

Thanks for the message!

Post count: 2

Here you have a sample code which I have little bit modified, so it should be easy for you to check the “meta_key” and the value of it that this code can run with your theme. It’s not so hard for sure for you to do it, as this code will make the “Views” colum sortable but it has small bugs, so if you can solve them would be good for the community for sure.

// Register the column
function td_post_views_column_register( $columns ) {
$columns[‘td_post_views’] = __( ‘Post Views’, ‘my-plugin’ );
return $columns;
}
add_filter( ‘manage_edit-post_columns’, ‘td_post_views_column_register’ );

// Display the column content
function td_post_views_column_display( $column_name, $post_id ) {
if ( ‘td_post_views’ != $column_name )
return;

$td_post_views = get_post_meta($post_id, ‘post_view_counter_key’, true);
if ( !$td_post_views )
$td_post_views = ‘‘ . __( ‘undefined’, ‘my-plugin’ ) . ‘‘;
echo $td_post_views;
}
add_action( ‘manage_posts_custom_column’, ‘td_post_views_column_display’, 10, 2 );

// Register the column as sortable
function td_post_views_column_register_sortable( $columns ) {
$columns[‘td_post_views’] = ‘td_post_views’;
return $columns;
}
add_filter( ‘manage_edit-post_sortable_columns’, ‘td_post_views_column_register_sortable’ );

function td_post_views_column_orderby( $vars ) {
if ( isset( $vars[‘orderby’] ) && ‘td_post_views’ == $vars[‘orderby’] ) {
$vars = array_merge( $vars, array(
‘meta_key’ => ‘post_view_counter_key’,
‘orderby’ => ‘meta_value_num’
) );
}
return $vars;
}
add_filter( ‘request’, ‘td_post_views_column_orderby’ );

PS: Th only problem which I am getting is the stuff with “get_post_meta” and the one for the “meta_key”, so please take a look it should work with your input and modification.

  • This reply was modified 9 years by skajic.
Post count: 23312

Hello skajic,

Please keep in mind that we cannot provide additional customizations through the code and also, this is not a simple task to do. We cannot offer you a solution in this regard because it means to do all of this task from scratch and it involves time-consuming and also, we cannot afford this, sorry! Notice that all of these requests which involve customizations are not covered by our support, as you can see in our support policy, like this -> https://themeforest.net/page/item_support_policy

Thanks for your understanding!

Viewing 4 posts - 1 through 4 (of 4 total)
The forum ‘Newspaper’ is closed to new topics and replies.