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.
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 themeta_key
line stops crashing, but then it also doesn’t sort properly.
Then guessing/hacking some more, I tried added youritem_list_query_join
function withLEFT JOIN
. That didn’t make any difference.
So I’m stuck. How do I make thepageviews
column sort with myadd_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.
- You must be logged in to reply to this topic.