In a previous tutorial, I showed you how to display a plain, non-sortable “Views” Column for Posts in the WordPress Dashboard. In this tutorial, I will show you how to make that “Views” Custom Column Sortable for Posts, ascending or descending based on the number of post views.
A WordPress developer asked me the following question: Is there a way to make the “Views” Custom Column sortable in the WordPress dashboard? And the answer is YES and here is how to make a Sortable Custom Column for Posts.
Before: using CODE-3 | WP Dashboard
The picture below show the Non-Sortable Views Column in the WordPress dashboard, when you implement CODE-3 from my previous tutorial.
After: using CODE-3A | WP Dashboard
The pictures below show the Sortable Views Column in the WordPress dashboard, after implementing CODE-3A discussed later.
My previous tutorial: How To Track & Display Post Views Count in WordPress Without a Plugin, is the basis for this post. Implementing (CODE-1 and CODE-2) or (CODE-1 and CODE-2a) of my previous tutorial is a MUST!
The difference will be:
- If you add CODE-3 from my previous tutorial, you will get a Non-Sorted Custom Views Column.
- However, if you add CODE-3A from this tutorial, you will get a Sorted Custom Views Column, descending or ascending by views count. A nice feature that will make the new custom column support sorting.
Enough with talking, show me the Code!
Sortable Post Views Column In WP Dashboard | functions.php
Open the functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-3A. Save the file and upload it to your server.
Then Login to your WordPress dashboard. In your Administration Screens go to the “Posts” tab, and you will notice a new Views column that displays the number of views for each post. What´s nice about it is that you can sort the Views column ascending or descending.
CODE-3A :: Sortable Custom Views Column for Posts in WP dashboard
Note: To scroll within the code: You can also click on the code window and use your keyboard´s arrow keys.
Some themes have more variations of the functions.php file, so look at their documentation and see where best to add the code.
CODE-3A requires (CODE-1 AND CODE-2) or (CODE-1 AND CODE-2a) of my previous tutorial.
If you added CODE-3A to your functions.php file, and you still don´t see the Views Column in your Dashboard, then make sure that your screen options is set to display the new column as shown in the image below:
The difference between CODE-3A and CODE-3 are lines 46 to 64 of the code.
CODE-3A works on WordPress 3.1 and higher (sortable custom columns in WordPress dashboard is a 3.1+ feature). But I hope that you will upgrade to the latest version.
If you want to remove the “Views” Column from the WordPress Dashboard, then you have 2 choices:
- Either delete CODE-3A from your theme´s functions.php file.
- Or Keep CODE-3A, but add the following function to the end of CODE-3A.
When you add several PHP code blocks in your theme´s funtions.php file, make sure that you do NOT leave any white space (spaces, newline) before the opening PHP tag (<?php) or after the closing PHP tag (?>). The correct way is as follows:
In the above code, if you leave any white space or a newline between lines 4 and 5, you will get the following error: “Warning: Cannot modify header information - headers already sent by…”
Your Turn to Talk
You now have the choice to show the Post Views Custom Column in your WordPress Dashboard in a Sortable format, and without a plugin. In the dashboard, the code adds a new Sortable Custom Column for Posts that tracks the number of views of your Posts.
How easy did you find this tutorial to implement? Do you have something to add or anything else to say? If so, please share your opinion in the comments section. Your opinion matters, unless it is a Spam.