Good day!
I try to make display post counter for WordPress.

Output count of views

function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
    return "0";    }
return $count.'';}

register view

function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
    $count = 0;
    delete_post_meta($postID, $count_key);
    add_post_meta($postID, $count_key, '0');
    update_post_meta($postID, $count_key, $count);    }}

output count of views in the admin panel

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'] = __('Views');
    return $defaults;}
function posts_custom_column_views($column_name, $id){
    if($column_name === 'post_views'){
        echo getPostViews(get_the_ID());    }}

Everything works!!!

Adds the ability to sort column

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;}

It becomes possible to sort by column "Views", by pressing the string has the form: /wp-admin/edit.php?orderby=views_views&order=desc But in fact sorting is not on the field "Views", it is on the field "Date".

Change the query for sorting columns show WordPress, that need sorting by random fields whose values are numbers
(Method 1)

add_filter('pre_get_posts', 'add_column_views_request');
function add_column_views_request( $object ){
  if( $object->get('orderby') != 'views_views' )
  $object->set('meta_key', 'post_views');
  $object->set('orderby', 'meta_value_num');}

(Method 2)

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' );

(Method 3)

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;}

Sort simply return no results: "No entries found."
Tell me how to fix the code, please.

Help me with this problem?
Thank you for your response.

1 Answer 1


I just should make
Method 1

 $object->set('meta_key', 'post_views_count');

Method 2

 $query->set( 'meta_key', 'post_views_count' );

Method 3

 $vars['meta_key'] = 'post_views_count';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.