Home User profile
tagDiv Member
This user did not write anything. So we are just showing here some random text to make the profile page look nice :)
skajic
tagDiv Member

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.
Viewing 1 post (of 1 total)