Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Sorting Custom User Columns WordPress

  • SOLVED

Trying to sort custom columns on the user.php screen, but can't seem to get it to work correctly. Hoping someone sees my error

Code:



function modify_user_table( $column ) {
$column['status'] = 'Status';
return $column;
}
add_filter( 'manage_users_columns', 'modify_user_table' );

function modify_user_table_row( $val, $column_name, $user_id ) {
$user = get_userdata( $user_id );
switch ($column_name) {
case 'status' :
$status = $user->status;
if ($status == 'Current'){
return '<div class="special2">'.$status.'</div>';
}else if ($status == 'Past Due'){
return '<div class="special1">'.$status.'</div>';
}else{ return '<div class="special">'.$status.'</div>'; }
break;
default:
}
switch ($column_name) {
case 'status' :
return $user->status;
break;
default:
}
return $return;
}
add_filter( 'manage_users_custom_column', 'modify_user_table_row', 10, 3 );

function status_column_sortable($columns) {
$custom = array(
'status' => 'status',
);
return wp_parse_args($custom, $columns);
}
add_filter( 'manage_users_sortable_columns', 'status_column_sortable' );

function status_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'status' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => 'status',
'orderby' => 'meta_value'
) );
}
return $vars;
}
add_filter( 'request', 'status_column_orderby' );



EDIT------------------------------------------

To clarify:

The column is 'appearing' as sortable, and the column is properly being filled with the right information

However, it is ordering by username (the default sort) and not by the actual content of the column

Answers (5)

2013-03-18

Navjot Singh answers:

Try changing

function status_column_sortable($columns) {
$custom = array(
'status' => 'status',
);
return wp_parse_args($custom, $columns);
}

to

function status_column_sortable( $columns ) {
$columns['status'] = 'status';
return $columns;
}


Kyle comments:

Hello, thanks for the reply. I tried this but it didn't work. I added some additional info up top.


Navjot Singh comments:

Try replacing


function status_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'status' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => 'status',
'orderby' => 'meta_value'
) );
}
return $vars;
}
add_filter( 'request', 'status_column_orderby' );

by

add_action('pre_user_query', 'status_column_orderby');
function status_column_orderby($userquery){
if('status'==$userquery->query_vars['orderby']) {//check if church is the column being sorted
global $wpdb;
$userquery->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) ";//note use of alias
$userquery->query_where .= " AND alias.meta_key = 'status' ";//which meta are we sorting with?
$userquery->query_orderby = " ORDER BY alias.meta_value ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order
}
}


Kyle comments:

THis almost works, but it doesn't display users with empty meta values


Kyle comments:

Is there someway to correct it so it doesn't drop off users with empty values?


Navjot Singh comments:

Drop users with empty meta values? From my testing, the users with empty meta values are still shown at the end or starting depending on the sort. Where's the issue?


Kyle comments:

Well, for example on the test site I have 7 users with example values for Status, but when I click to orderby status, it is only displaying 5 users (the 2 users with empty values are not visible).


Navjot Singh comments:

Can you PM me your login?


Kyle comments:

Unfortunately no, this is a client's site


Navjot Singh comments:

I created 3 users and added the same meta value using the plugin you mentioned but can't reproduce. Can you show me the screenshot of the user page after you click sort? What happens? And which WP version you using?


Kyle comments:

Here is a clean screenshot, notice only 5/7 users are displayed when sorting by Status

http://imgur.com/y61dQlB


Navjot Singh comments:

No idea. Can't reproduce it here. Try deactivating your plugins 1 by 1 except the User meta one and see if that helps in identifying the problem.


Kyle comments:

I set a default value of ' ' for the meta field and that solved it. Thanks for your help!

2013-03-18

Daniel Yoen answers:

try to use 'pre_get_posts' instead 'request'.

add_action( 'pre_get_posts', 'status_column_orderby' );

hope this help :-)


Kyle comments:

Thanks for the reply, no luck though


Daniel Yoen comments:

complete code :-)

add_action( 'pre_get_posts', 'event_column_orderby' );
function event_column_orderby($query)
{
$orderby = $query->get('orderby');

if('status' == $orderby)
{
$query->set('meta_key','status');
$query->set('orderby','meta_value');
}
}


hope this help :-)


Kyle comments:

Still no luck

2013-03-18

asok answers:

I'm looking to see if there is a simpler/efficient way.

but isn't the column name user_status. Tried to edit your code to use 'user_status' as the column name, instead. See if it works.


function modify_user_table( $column ) {
$column['user_status'] = 'Status';
return $column;
}
add_filter( 'manage_users_columns', 'modify_user_table' );

function modify_user_table_row( $val, $column_name, $user_id ) {
$user = get_userdata( $user_id );
switch ($column_name) {
case 'user_status' :
$status = $user->user_status;
if ($status == 'Current'){
return '<div class="special2">'.$status.'</div>';
}else if ($status == 'Past Due'){
return '<div class="special1">'.$status.'</div>';
}else{ return '<div class="special">'.$status.'</div>'; }
break;
default:
}

switch ($column_name) {
case 'user_status' :
return $user->user_status;
break;
default:
}
return $return;
}
add_filter( 'manage_users_custom_column', 'modify_user_table_row', 10, 3 );

function status_column_sortable($columns) {
$custom = array(
'user_status' => 'user_status',
);
return wp_parse_args($custom, $columns);
}
add_filter( 'manage_users_sortable_columns', 'status_column_sortable' );

function status_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'status' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => 'user_status',
'orderby' => 'meta_value'
) );
}
return $vars;
}
add_filter( 'request', 'status_column_orderby' );


Kyle comments:

Interesting... technically this worked, but it does not display users with empty meta_values for the column

2013-03-18

Arnav Joy answers:

try this

function modify_user_table( $column ) {

$column['status'] = 'status';

return $column;

}

add_filter( 'manage_users_columns', 'modify_user_table' );



function modify_user_table_row( $val, $column_name, $user_id ) {

$user = get_userdata( $user_id );

switch ($column_name) {

case 'status' :

$status = $user->status;

if ($status == 'Current'){

return '<div class="special2">'.$status.'</div>';

}else if ($status == 'Past Due'){

return '<div class="special1">'.$status.'</div>';

}else{ return '<div class="special">'.$status.'</div>'; }

break;

default:

}

switch ($column_name) {

case 'status' :

return $user->status;

break;

default:

}

return $return;

}

add_filter( 'manage_users_custom_column', 'modify_user_table_row', 10, 3 );



function status_column_sortable($columns) {

$custom = array(

'status' => 'status',

);

return wp_parse_args($custom, $columns);

}

add_filter( 'manage_users_sortable_columns', 'status_column_sortable' );



function status_column_orderby( $vars ) {

if ( isset( $vars['orderby'] ) && 'status' == $vars['orderby'] ) {

$vars = array_merge( $vars, array(

'meta_key' => 'status',

'orderby' => 'meta_value'

) );

}

return $vars;

}

add_filter( 'request', 'status_column_orderby' );


Kyle comments:

Can you please specify what changes you made


Arnav Joy comments:

i changed this line


$column['status'] = 'Status';

to

$column['status'] = 'status';


Kyle comments:

Oh okay, no luck there

Thanks for the reply btw

2013-03-18

Dbranes answers:

Hi, I just wanted to join the sorting club here ;-)

Here is another way to sort the custom column:

[[LINK href="http://j3webworks.com/blog/wordpress-add-and-sort-custom-column-in-users-admin-page"]]http://j3webworks.com/blog/wordpress-add-and-sort-custom-column-in-users-admin-page[[/LINK]]

By using the above info, with some modifications, I can get your code to work, if I add this part:

add_action('pre_user_query', 'status_order_in_user_query');
function status_order_in_user_query($query){
// print_r($query); // for debugging
if('status'==$query->query_vars['orderby']) {
$query->query_orderby = " ORDER BY user_status ".($query->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order
}
}


I just order by the <strong>user_status</strong> field in the <strong>wp_users</strong> table (not doing any meta_value ordering)

Hope this helps ;-)

<strong>PS:</strong> I just had to replace <strong>$user->status</strong> with <strong>$user->user_status,</strong>, in your code, to get the status values on my single WP install.


Kyle comments:

Thanks for the reply

No accurate result though


Dbranes comments:

ok, looks like your setup is different ;-)

ps: just curious, did you add a new <strong>status</strong> column in the wp_users table?

$user = get_userdata( $user_id );
print_r($user);


does not give me any <strong>status</strong> value.


Kyle comments:

Yes I did, should have said that explicitly

Did so with this: http://wordpress.org/extend/plugins/user-meta-manager/

Pretty well made plugin, nice ajax actions


Dbranes comments:

ok, that explains it ;-)
'
so you actually need user meta sorting instead of my user_status field sortering

You can check out the link I provided above, it has the original code used by myself and @Navjot.

... thanks for the info