Reputation: 5483
I want to list all authors and order them from A-Z. The problem is, that I need to order them by a custom meta field and not their display name.
I have everything working if I order the authors by their display name. But if I try to order them by a custom field, the A-Z listing doesn't work anymore.
For context: I'm using the WooCommerce plugin WC Vendors and the authors have a shop name which I want to use as reference for the ordering.
Here's my current code:
$display_admins = false;
$order_by = 'display_name'; // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'
$role = 'vendor'; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all'
$avatar_size = 32;
$hide_empty = true; // hides authors with zero posts
$last = '';
if(!empty($display_admins)) {
$blogusers = get_users('orderby='.$order_by.'&role='.$role);
} else {
$admins = get_users('role=administrator');
$exclude = array();
foreach($admins as $ad) {
$exclude[] = $ad->ID;
}
$exclude = implode(',', $exclude);
$blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&role='.$role);
}
$authors = array();
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->ID);
if(!empty($hide_empty)) {
$numposts = count_user_posts($user->ID, 'product');
if($numposts < 1) continue;
}
$authors[] = (array) $user;
}
echo '<ul class="contributors">';
foreach($authors as $author) {
//$display_name = $author['data']->display_name;
//$avatar = get_avatar($author['ID'], $avatar_size);
//$author_profile_url = get_author_posts_url($author['ID']);
$vendor_shop_name = get_user_meta( $author['ID'], 'pv_shop_name', true );
$vendor_shop_desc = get_user_meta( $author['ID'], 'pv_shop_description', true );
$vendor_shop_keyvsiual = get_user_meta( $author['ID'], 'vendor_keyvisual', true );
$vendor_shop_icon_id = get_user_meta( $author['ID'], '_wcv_store_icon_id', true );
$vendor_shop_link = WCV_Vendors::get_vendor_shop_page( $author['ID'] );
$vendor_shop_address1 = get_user_meta( $author['ID'], '_wcv_store_address1', true );
$vendor_shop_address2 = get_user_meta( $author['ID'], '_wcv_store_address2', true );
$vendor_shop_city = get_user_meta( $author['ID'], '_wcv_store_city', true );
$vendor_shop_postcode = get_user_meta( $author['ID'], '_wcv_store_postcode', true );
$vendor_shop_country = get_user_meta( $author['ID'], '_wcv_store_country', true );
// This works fine
$current = strtolower($author['data']->display_name[0]);
// This does not work
//$vendor_shop_name_start = substr($vendor_shop_name, 0, 1);
//$current = strtolower($vendor_shop_name_start);
if ($last != $current) {
echo '<a class"alphabit" name="' . strtoupper($current) . '">' . strtoupper($current) . '</a>';
$last = $current;
}
echo '<li><a href="', $vendor_shop_link, '" class="contributor-link">', $vendor_shop_name, '</a></li>';
}
echo '</ul>';
As you can see, I already tried to change $current
to the meta field pv_shop_name
.
And I reduced the string to the first letter. Which works fine but for some reason the ordering doesn't work anymore. And I saw a problem with a special character if the shop name start with an Ö
for example.
I don't see the problem here.
Maybe it has something to do with the $order_by = 'display_name';
at the beginning?
But how can I change that?
Upvotes: 1
Views: 177
Reputation: 5483
I figured it out.
I have to change the $oderby
to:
$order_by = 'meta_value';
And I had to add meta_key=pv_shop_name
to $blogusers
:
$blogusers = get_users('meta_key=pv_shop_name&exclude='.$exclude.'&orderby='.$order_by.'&role='.$role);
Now it works fine.
Upvotes: 1