Sorting admin columns with meta_value
-
Hello! I have been working on a plugin to add custom columns to users table and then make them sortable by meta_value and meta_key. So far I got everything working except the last bit, to make them sort based on the meta_value of a chosen meta_key. I don’t know what I’m doing wrong. The code section I think that is at fault is immediately below, and my entire code is at the bottom.
The name of my meta_key in the database is ‘last_name’ and the name of the column header is ‘LastName’ and I want it to sort alphabetically in ascending and descending order as you click it. It keeps sorting and always has sorted alphabetically based on the username in Column 1, no matter which sort column I click. That is the problem. I am apparently not able to change the query variables array. I’m sure my error is pretty simple. I have tried different variations of the code below.
Thanks a ton!!!
// SORT BLOCK // add columns to sortable columns array function sort_mishas_user_columns ($columns){ $columns["FirstName"] = "FirstName"; $columns["LastName"] = "LastName"; return $columns; } add_filter('manage_users_sortable_columns', 'sort_mishas_user_columns'); // the hook // modify/merge sort criteria array with new criteria --- I think there's an error here, something not working. It sorts by the username still function sort_az_users($vars) { if(array_key_exists('orderby', $vars)) { // Assuming this is correct and auto-created by the WP hook below if('LastName' == $vars['orderby']) { // 'LastName' is the column header name that is clickable. Assuming it gets set to that on "click" $vars['orderby'] = 'meta_value'; $vars['meta_key'] = 'last_name'; // 'last_name' is the name in the database under 'meta_key' } } return $vars; } add_filter('request', 'sort_az_users'); // the hook // END SORT BLOCKMy entire code
<?php /** * Plugin Name: j-w-m * Plugin URI: http://www.journeywithmisha.com/j-w-m * Description: The very first plugin that I have ever created. * Version: 1.0 * Author: Gerald Ryan * Author URI: http://www.webbuildermn.com */ function add_custom_misha_columns($columns){ $columns["Nova"] = "Nova"; $columns["Brilliant.org"] = "Brilliant.org"; $columns["FirstName"] = "FirstName"; $columns["LastName"] = "LastName"; return $columns; } add_filter('manage_users_columns', 'add_custom_misha_columns'); // the hook // SORT BLOCK // add columns to sortable columns array function sort_mishas_user_columns ($columns){ $columns["FirstName"] = "FirstName"; $columns["LastName"] = "LastName"; return $columns; } add_filter('manage_users_sortable_columns', 'sort_mishas_user_columns'); // the hook // modify/merge sort criteria array with new criteria --- I think there's an error here, something not working. It sorts by the username still function sort_az_users($vars) { if(array_key_exists('orderby', $vars)) { // Assuming this is correct and auto-created by the WP hook below if('LastName' == $vars['orderby']) { // 'LastName' is the column header name that is clickable. Assuming it gets set to that on "click" $vars['orderby'] = 'meta_value'; $vars['meta_key'] = 'last_name'; // 'last_name' is the name in the database under 'meta_key' } } return $vars; } add_filter('request', 'sort_az_users'); // the hook // END SORT BLOCK // // Populate the column function add_content_to_mishas_column($value, $column_name, $user_id){ global $wpdb; $fname = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE user_ID = $user_id AND meta_key = 'first_name' " ); $lname = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE user_ID = $user_id AND meta_key = 'last_name' " ); if ($column_name == "FirstName"){ return $fname; // return "Romeo"; } if ($column_name == "LastName"){ return $lname; // return "Montague"; } if ($column_name == "Brilliant.org"){ return $user_id; } if ($column_name == "Nova"){ return $value; } } add_action('manage_users_custom_column', 'add_content_to_mishas_column', 10, 3);
The topic ‘Sorting admin columns with meta_value’ is closed to new replies.