Title: Sorting views column in admin
Last modified: September 27, 2024

---

# Sorting views column in admin

 *  Resolved [Jim Reekes](https://wordpress.org/support/users/reekes/)
 * (@reekes)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/sorting-views-column-in-admin/)
 * Hello Hector,
   I want to move off Top 10 and start using your plugin. I’ve moved
   the database tables over, and that’s working. I want to add the same sorting 
   of the Views column I once had (which Top 10 has removed in a recent update).
   I’ve read the [How to: Add Sortable Views Column to Posts / Pages / CPTs Admin Screen](https://wordpress.org/support/topic/how-to-add-sortable-views-column-to-posts-pages-cpts-admin-screen/).
   When I added that code unmodified to WPCode as a snippet, the entire WordPress
   site crashed. That approach is a bit different from mine. So instead of going
   further with that code, I’m trying to make the changes in the code I already 
   had working. I’m struggling to the point of just hacking. I already have a code
   snippet I’m using to provide columns and sorting, and it had working with the
   Top 10’s views column. So I’m really just trying to replace the values in that
   column.Sorry to bother you with a request to help with my code. My PHP skills
   are average at best. I don’t know what’s going wrong. I do think I’m close!I 
   have the column header and populating the column with the correct ‘pageviews’
   value from Popular Posts. But, I’m not getting a correct sort.My existing code
   for sorting columns:`add_filter('request', 'my_column_sort');function my_column_sort(
   $vars) {if (!is_admin()) { return; }if (isset( $vars['orderby'] )) {Then I test
   for the column being requested, and the code I thought would work:if ('pageviews'
   == $vars['orderby']) { $vars = array_merge( $vars, array( 'meta_key' => 'pageviews','
   orderby' => 'meta_value_num' ));}But that causes the entire function to fail.
   If I comment out the meta_key line stops crashing, but then it also doesn’t sort
   properly.Then guessing/hacking some more, I tried added your item_list_query_join
   function with LEFT JOIN. That didn’t make any difference.So I’m stuck. How do
   I make the pageviews column sort with my add_filter('request', 'my_column_sort')
   function?PS – I’ve not used name space at all for any functions (ever). I couldn’t
   tell you if/when I would ever need that.
    -  This topic was modified 1 year, 8 months ago by [Jim Reekes](https://wordpress.org/support/users/reekes/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/sorting-views-column-in-admin/#post-18041985)
 * Hey [@reekes](https://wordpress.org/support/users/reekes/),
 * > When I added that code unmodified to WPCode as a snippet, the entire WordPress
   > site crashed.
 * That’s not supposed to be used as a code snippet but as a standalone plugin as
   mentioned [here](https://gist.github.com/cabrerahector/138a1926cf8d764a4817e7ff396c9219#how-do-i-add-this-to-my-site).
   I’ll test it out though since it’s been a good minute since I last used it, maybe
   it needs some updates?
 * **Edit:** Just tried it out, it works as expected here. No issues at all. Please
   make sure to follow the [instructions](https://gist.github.com/cabrerahector/138a1926cf8d764a4817e7ff396c9219#how-do-i-add-this-to-my-site)
   and try again. If your site “crashes” again after doing that please [grab a log](https://wordpress.org/support/article/debugging-in-wordpress/)
   and post its content somewhere (eg. pastebin.com) and I’ll have a look.
 * > How do I make the pageviews column sort with my add_filter(‘request’, ‘my_column_sort’)
   > function?
 * I’m not familiar with this ‘request’ hook nor how it works so I couldn’t say.
 *  Thread Starter [Jim Reekes](https://wordpress.org/support/users/reekes/)
 * (@reekes)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/sorting-views-column-in-admin/#post-18042034)
 * > not supposed to be used as a code snippet
 * Understood. I did try using the original code unmodified (except for changing
   the post type to my CPT). I only use WP Code for adding such snippets. Putting
   the code there it crashed the site to where I couldn’t even get to the admin 
   pages.
 * Also, I have some custom fields in my custom post. Those I’ve got as columns 
   in the admin page. So I need to merge that with Popular Post’s pageviews.
 * I’ve struggled with sorting. I’ve been trying to add the custom post count to
   the admin’s User listing, and haven’t gotten that working either.
 * > not familiar with this ‘request’ hook
 * It was one of many options I tried, and it was the only one I got working for
   the other columns. I have no doubt there’s a better approach.
 * What filter should I hook on for sorting different columns within the Custom 
   Posts listing? I would assume that’s what I should also be using for my User 
   posts column.
 * I’ll try getting the original code you provided working as-is, again.
 * I’ll also try to find another way to hook on the sorting option. But, I’ve already
   tried several methods and the ‘request’ hook was the only one I got working. 
   It’s a switch with a case (or if-else) for each column type.
 * > please [grab a log](https://wordpress.org/support/article/debugging-in-wordpress/)
 * I think this was the crash, but it looks like a side effect.
 *     ```wp-block-code
       [27-Sep-2024 21:24:38 UTC] PHP Fatal error:  Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, function "custom_post_type_articles" not found or invalid function name in mysite.com/public_html/wp-includes/class-wp-hook.php:324Stack trace:#0 mysite.com/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)#1 mysite.com/public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)#2 mysite.com/public_html/wp-settings.php(700): do_action('init')#3 mysite.com/public_html/wp-config.php(94): require_once('/home/customer/...')#4 mysite.com/public_html/wp-load.php(50): require_once('/home/customer/...')#5 mysite.com/public_html/wp-blog-header.php(13): require_once('/home/customer/...')#6 mysite.com/public_html/index.php(17): require('/home/customer/...')#7 {main}  thrown in mysite.com/public_html/wp-includes/class-wp-hook.php on line 324
       ```
   
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/sorting-views-column-in-admin/#post-18042058)
 * From the log it seems that your site isn’t happy about you trying to load plugin’s
   code via WP Code.
 * You probably have a good reason to try doing all this your own way with WP Code
   vs using a custom plugin as I recommend. If you ever figure out what’s wrong 
   with your code and manage to get it working please consider leaving a comment
   here, it might help a future reader who also happens to prefer not using a plugin
   for this.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Sorting views column in admin’ is closed to new replies.

 * ![](https://ps.w.org/wordpress-popular-posts/assets/icon-256x256.png?rev=1232659)
 * [WP Popular Posts](https://wordpress.org/plugins/wordpress-popular-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wordpress-popular-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wordpress-popular-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wordpress-popular-posts/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * Last activity: [1 year, 8 months ago](https://wordpress.org/support/topic/sorting-views-column-in-admin/#post-18042058)
 * Status: resolved