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

$orderby users to sort on nickname field WordPress

  • SOLVED

http://clients.screengroove.com/2im/?page_id=6

Objective: Sort users by nickname ascending

I am trying to control the order by which the users are listed and modified the $orderby to nickname in the theme-functions.php, but the page is not respecting this or any other $orderby value

The idea is to use the user nickname field to assign A,B,C, etc so as to dictate the order

From theme-functions.php

// Get users
function ct_get_users($users_per_page = 10, $paged = 1, $role = '', $orderby = 'nickname', $order = 'ASC', $usersearch = '' ) {

global $blog_id;

$args = array(
'number' => $users_per_page,
'offset' => ( $paged-1 ) * $users_per_page,
'role' => $role,
'search' => $usersearch,
'fields' => 'all_with_meta',
'blog_id' => $blog_id,
'orderby' => $orderby,
'order' => $order
);

$wp_user_search = new WP_User_Query( $args );
$user_results = $wp_user_search->get_results();

return $user_results;

}



From template-team.php

<?php
/**
* Template Name: Team
*
* @package WP Executive
* @subpackage Template
*/

get_header(); ?>

<section id="content" class="left">

<article class="clear">
<?php include(TEMPLATEPATH . '/includes/post-lead.php'); ?>
<h1><?php the_title(); ?></h1>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>

<?php if ( get_query_var('paged') ) $paged = get_query_var('paged'); elseif ( get_query_var('page') ) $paged = get_query_var('page'); else $paged = 1; ?>
<?php
$user_list = ct_get_users( 10, $paged );
$count = 0;

foreach($user_list as $author) {

if (get_the_author_meta('exclude',$author->ID) != 10) {

$curauth = get_userdata($author->ID);
if ($curauth->user_level != 2) continue;

$author_id = $author->ID;
$author_name = $author->display_name;
/* custom profile fields */
$author_title = $author->title;
$author_profile_img = $author->ct_profile_url;
$author_twitter = $author->twitter;
$author_facebook = $author->facebook;
$author_linkedin = $author->linkedin;
$author_google = $author->google;
$author_page_url = get_author_posts_url($author_id);

$count++;
?>

<article class="member">
<?php if($author_profile_img) { ?><img class="author-img left" src="<?php echo get_template_directory_uri(); ?>/img_resize/timthumb.php?src=<?php echo $author_profile_img; ?>&h=129&w=95&zc=1" /><?php } ?>
<div class="author-info right">
<h2><?php echo $author_name; ?></h2>
<?php if ($author_title) { ?><h3><?php echo $author_title; ?></h3><?php } ?>
<p><?php the_author_meta( 'description', $author->ID ); ?></p>
<ul>
<?php if ($author_twitter) { ?><li class="twitter"><a href="http://twitter.com/#!/<?php echo $author_twitter; ?>" target="_blank">@<?php echo $author_twitter; ?></a></li><?php } ?>
<?php if ($author_facebook) { ?><li class="facebook"><a href="<?php echo $author_facebook; ?>" target="_blank"><?php _e( 'Facebook', 'contempo' ); ?></a></li><?php } ?>
<?php if ($author_linkedin) { ?><li class="facebook"><a href="<?php echo $author_linkedin; ?>" target="_blank"><?php _e( 'LinkedIn', 'contempo' ); ?></a></li><?php } ?>
<?php if ($author_google) { ?><li class="google"><a href="<?php echo $author_google; ?>" target="_blank"><?php _e( 'Google+', 'contempo' ); ?></a></li><?php } ?>
</ul>
</div>
</article>

<?php
} // End If Statement
} // End For Loop

?>

</article>

</section>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Answers (3)

2011-10-30

Jurre Hanema answers:

The nickname you can enter in the user admin is not, in fact, the user_nicename. It is stored as user metadata in the usermeta table with key 'nickname'. It is currently not possible to sort users on nickname using WP_User_Query.

Modifying WP_User_Query to allow this would be somewhat problematic, so what you could do is to perform the sort afterwards:



// Function to compare two users
function wpq_cmp_user($a, $b)
{
$a = (int)$a->nickname;
$b = (int)$b->nickname;

if($a == $b)
return 0;
if($a < $b)
return -1;
return 1;
}


// Get users

function ct_get_users($users_per_page = 10, $paged = 1, $role = '', $orderby = 'nickname', $order = 'ASC', $usersearch = '' ) {

global $blog_id;

$args = array(
'number' => $users_per_page,
'offset' => ( $paged-1 ) * $users_per_page,
'role' => $role,
'search' => $usersearch,
'fields' => 'all_with_meta',
'blog_id' => $blog_id,
'orderby' => $orderby,
'order' => $order
);

$wp_user_search = new WP_User_Query( $args );

$user_results = $wp_user_search->get_results();

usort($user_results, 'wpq_cmp_user');

return $user_results;
}


This will work provided that you give your user nicknames like 1, 2, 3... (not A, B, C...)

NOTE: It just sprung to my mind that this will probably fail as soon as you start using pagination for your user list. Not much to do about that I'm afraid (at least not for $10)...

2011-10-29

rizaljohn answers:

Is the nickname a custom user field? Or did you mean orderby 'user_nicename'?


PATRICK IWANICKI comments:

'user_nicename' or other values do not seem to be working.

'user_nicename' is value in place now

2011-10-30

Baki Goxhaj answers:

I was working on such an issue a couple of days ago. From the Class definition WP_User_Query you can only order by these values:

1. user_nicename
2. user_email
3. user_url
4. user_registered
5. display_name
6. post_count
7. ID
8. user_login (the default)

I guess you want to use <strong>use_nicename</strong> rather than <strong>nickname</strong>, which is the default value at your function.