Answer the question
In order to leave comments, you need to log in
How to display a sortable column in the WordPress admin for a custom field that counts the number of post views?
Good afternoon.
Implemented WordPress article views counter ( source )
/* displaying the number of views */
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0"; }
return $count.'';}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count); }}
add_filter('manage_pages_columns', 'posts_column_views');
add_action('manage_pages_custom_column', 'posts_custom_column_views',10,2);
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',10,2);
function posts_column_views($defaults){
$defaults['post_views'] = __('Показы');
return $defaults;}
function posts_custom_column_views($column_name, $id){
if($column_name === 'post_views'){
echo getPostViews(get_the_ID()); }}
add_filter('manage_edit-post_sortable_columns', 'add_views_sortable_column');
add_filter('manage_edit-page_sortable_columns', 'add_views_sortable_column');
add_filter('manage_edit-slides_sortable_columns', 'add_views_sortable_column');
add_filter('manage_edit-hp_highlights_sortable_columns', 'add_views_sortable_column');
add_filter('manage_edit-portfolio_sortable_columns', 'add_views_sortable_column');
add_filter('manage_edit-staff_sortable_columns', 'add_views_sortable_column');
add_filter('manage_edit-services_sortable_columns', 'add_views_sortable_column');
add_filter('manage_edit-testimonials_sortable_columns', 'add_views_sortable_column');
add_filter('manage_edit-faqs_sortable_columns', 'add_views_sortable_column');
function add_views_sortable_column($sortable_columns){
$sortable_columns['post_views'] = 'views_views';
return $sortable_columns;}
add_filter('pre_get_posts', 'add_column_views_request');
function add_column_views_request( $object ){
if( $object->get('orderby') != 'views_views' )
return;
$object->set('meta_key', 'post_views');
$object->set('orderby', 'meta_value_num');}
add_action( 'pre_get_posts', 'add_column_views_request', 1 );
function add_column_views_request( $query ) {
if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
switch( $orderby ) {
case 'views_views':
$query->set( 'meta_key', 'post_views' );
$query->set( 'orderby', 'meta_value' );
break;}}}
add_filter('request', 'add_column_views_request');
function add_column_views_request( $vars ) {
if ( 'views_views' == $vars['orderby'] ){
$vars['meta_key'] = 'post_views';
$vars['orderby'] = 'meta_value_num';}
return $vars;}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question