Zach Nicodemous
Zach Nicodemous

Reputation: 9497

WordPress - User Query - Order by Meta Value

I am trying to use a WordPress User Query to create a list of users that is ordered by a custom meta value. Its a simple numeric value, going from 1 to 100, so 1 needs to be displayed first, 100 last, etc.

This is my attempt, which has failed miserably:

<?php

    $args  = array(
        'role' => 'Author',
        'meta_key' => 'order-number',
        'orderby' => 'order-number',
        'order' => 'asc',  
    );

$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();

if (!empty($authors))
{
    echo '<div style="float:left;">';
    foreach ($authors as $author)
    {
        $author_info = get_userdata($author->ID);
        echo '<div class="post team-member"><div class="image">';
        echo get_avatar( $author_info->ID, 91 );
        echo '</div>';
        echo '<div class="content"><div class="title"><a href="/author/' . $author_info->user_login . '">' . $author_info->user_firstname . ' ' . $author_info->user_lastname . '</a></div>';
        echo '' . substr( get_the_author_meta('user_description',$author_info->ID) , 0 , 100 ) . '...'; 
        echo '</div></div>';
    }
    echo '</div>';
} else {
    echo 'No authors found';
}

    ?>

I think the problem is that wp_user_query does not support custom fields in orderby - so I need a solution that works around this.

Any ideas?

Upvotes: 1

Views: 7426

Answers (2)

Anchal Sharma
Anchal Sharma

Reputation: 21

<?php

// prepare arguments this is your query.
$args = array(
'meta_key' => 'last_name',
'query_id' => 'wps_last_name',
);

// Create the WP_User_Query object
$author_query = new WP_User_Query( $args );

?>

Add following lines to functions.php file

<?php

add_action( 'pre_user_query', 'wps_pre_user_query' );
/*
* Modify the WP_User_Query appropriately
*
* Checks for the proper query to modify and changes the default user_login for $wpdb->usermeta.meta_value
*
* @param WP_User_Query Object $query User Query object before query is executed
*/
function wps_pre_user_query( &$query ) {
global $wpdb;
if ( isset( $query->query_vars['query_id'] ) && 'wps_last_name' == $query->query_vars['query_id'] )
$query->query_orderby = str_replace( 'user_login', "$wpdb->usermeta.meta_value", $query->query_orderby );
}
?>

Upvotes: 2

maiorano84
maiorano84

Reputation: 11971

Correct. The orderby parameter can only accept possible values of 'login' (default), 'nicename', 'email', 'url', and 'registered'.

A dirty fix would be something like this:

<?php
global $wpdb; //Ignore this line if you're not within a function
$order = $wpdb->get_results("SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key='order-number' ORDER BY meta_value ASC", "ARRAY_N");
$authors = array();
foreach($order as $aid)
    $authors[] = new WP_User($aid[0]);

if (!empty($authors))
{
    echo '<div style="float:left;">';
    foreach ($authors as $author)
    {
        $author_info = get_userdata($author->ID);
        echo '<div class="post team-member"><div class="image">';
        echo get_avatar( $author_info->ID, 91 );
        echo '</div>';
        echo '<div class="content"><div class="title"><a href="/author/' . $author_info->user_login . '">' . $author_info->user_firstname . ' ' . $author_info->user_lastname . '</a></div>';
        echo '' . substr( get_the_author_meta('user_description',$author_info->ID) , 0 , 100 ) . '...'; 
        echo '</div></div>';
    }
    echo '</div>';
} else {
    echo 'No authors found';
}

This is largely untested, so I can't guarantee this will work straight out of the gate, but it should get you started.

Hope this helps!

Upvotes: 3

Related Questions