0

I have a custom field for view counts that are stored as numbers. I added a Views column, and made it sortable, but they are not sorting properly.

The results when I click on the Views column are something like this:

Views
=====
1900
10992
10004
280
18
4015
...

Here is my code:

/**
 * Add view counts to admin columns
 */
add_filter( 'manage_posts_columns', 'eri_posts_column_views' );
function eri_posts_column_views( $columns ) {
    $columns['post_views'] = 'Views';
    return $columns;
}
add_action( 'manage_posts_custom_column', 'eri_posts_custom_column_views' );
function eri_posts_custom_column_views( $column ) {
    if ( $column === 'post_views') {
        $post_id = get_the_ID();
        $meta_key = 'eri_post_view_count';
        $count = 0;
        if (get_post_meta( $post_id, $meta_key, true ) && get_post_meta( $post_id, $meta_key, true ) != '') {
            $count = get_post_meta( $post_id, $meta_key, true );
        }
        echo $count;
    }
}


/**
 * Make admin columns sortable
 */
add_filter('manage_edit-post_sortable_columns', 'eri_view_count_sortable_column');
function eri_view_count_sortable_column($columns){
    $columns['post_views'] = 'post_views';
    return $columns;
}
add_action( 'pre_get_posts', 'eri_view_count_custom_orderby' );
function eri_view_count_custom_orderby( $query ) {
    if( ! is_admin() )
        return;

    $orderby = $query->get( 'orderby');

    if( 'post_views' == $orderby ) {
      $query->set('meta_key','eri_post_view_count');
      $query->set('orderby','meta_value_num');
    }
}

I've tried changing meta_value_num to meta_value... not working.

3
  • Can't reproduce the issue using the code you provided. Everything works for me. The only problem: when you order by View Count then all posts having no eri_post_view_count key disappears from the list until you order posts by something different.
    – Max Yudin
    Nov 11, 2021 at 11:54
  • Hmm.. I wonder why it's not working for me. I printed the $query and it shows what I set in the array, but I'm wondering if something else is overriding it. Also, all posts have the meta key; when a new post is created it starts with 0. I appreciate the response, though.
    – Michael
    Nov 11, 2021 at 15:07
  • @MaxYudin, I just tried my code on a different site and it works fine. Something is messing with it on the site I need it on. :( Any suggestions on where to look?
    – Michael
    Nov 11, 2021 at 15:30

2 Answers 2

0

I figured it out after adding the same code to a different site I run. It was working fine on that site with the same theme and mostly the same plugins. I narrowed it down to another function I made (thankfully it wasn't another plugin). The other function was sorting something on the front end, so all I had to do was add if (!is_admin()) to that function. So the moral of the story is, I screwed up in a completely different area. :(

-1

Can you please set query as per below code?

$query->set('orderby','meta_value meta_value_num');

or below:

$query->set('orderby','meta_value');
1
  • I mentioned in my post that I already tried that and it didn't work. Thanks, though.
    – Michael
    Nov 11, 2021 at 15:04

Your Answer

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

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