R
R
Roman2015-03-20 20:36:31
PHP
Roman, 2015-03-20 20:36:31

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

/* register view */
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);    }}

/* display the number of views in the admin */
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());    }}

Everything is working.
/* add the ability to sort the column */ Source
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 the "Impressions" column, when clicked, the line looks like:
/wp-admin/edit.php?orderby=views_views&order=desc
But in fact, sorting occurs not by the "Impressions" field, but by the "Date" field.
/* change the query when sorting the column, showing WordPress that it needs to sort by custom 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' )
    return;
  $object->set('meta_key', 'post_views');
  $object->set('orderby', 'meta_value_num');}

or (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' );
        break;}}}

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

Sorting just doesn't work: "No records found."
Please tell me how to fix the code.
What could be the problem?
Thank you for your reply.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2015-03-20
@llgruff

you store in 'post_views_count' field and sort by 'post_views'

#(Способ 1)
 $object->set('meta_key', 'post_views_count');
#(Способ 2)
 $query->set( 'meta_key', 'post_views_count' );
#(Способ 3)
 $vars['meta_key'] = 'post_views_count';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question