Sort get_users by custom field

I have a custom USER taxonomy called ‘label’ which also has a custom field of ‘sort_order’ – I am trying to return a list of users that have the label taxonomy and then sort this list by the sort_order field.

So far I have…..

Read More
<?php
$users = get_users(
array(
    'meta_key' => 'sort_order',
    'fields' => 'all_with_meta'
)
);

function wpse98580_sort_order( $a, $b )
{  
    if ( $a->sort_order === $b->sort_order ) {  
    return 0;  
} elseif ( $a->sort_order > $b->sort_order ) {
    return -1;
}
return 1;  
}  
usort( $users, 'wpse98580_sort_order' );

/* Iterate over the sorted array */

foreach( $users as $user )
{
echo '<h4>' . $user_info-> user_firstname . ' ' . $user_info-> user_lastname . '</h4>';
}
?>

This doesn’t work, I am assuming it is becasue the ‘sort_order’ meta-key is actually a custom field of the taxonomy so it can’t retrieve it. Usually I use the following code to access the sort_order…

$product_terms = wp_get_object_terms($user->ID, 'label');
                    if(!empty($product_terms)){
                    if(!is_wp_error( $product_terms )){
                        foreach($product_terms as $term){

                        $t_ID = $term->term_id;
                        $label_custom_fields = get_option("taxonomy_term_$t_ID");
                        echo $label_custom_fields['sort_order'];

                        }
                        }
                    }

Can anyone help?

Related posts

3 comments

  1. By using this you can get your result in ascending order for sort_order custom field

    $users = get_users(
       array(
       'meta_key'=> 'sort_order',
       'orderby' => 'meta_value_num',
       'order'   => 'ASC'
       )
    );
    
    foreach( $users as $user )
    {
       echo '<h4>' . $user_info-> user_firstname . ' ' . $user_info-> user_lastname . '</h4>';
    }
    
  2. I notice that get_users does not support this.

    Unfortunately, it does not take meta_value or meta_value_num as a value for the orderby parameter, as its post-related companions do.

    But we can specify to only grab those with a value for the meta_key sort_order set:

    $users = get_users(
        array(
            'meta_key' => 'sort_order',
            'fields' => 'all_with_meta'
        )
    );
    

    get_users() returns an array of user objects (instances of the WP_User class).
    Though a var_dump of one of those objects will not reveal it, metadata of the user is accessible as a property thanks to the class’s magic methods.

    Hence – though untested – the following should sort the users the way you desire:

    function wpse98580_sort_order( $a, $b )
    {  
        if ( $a->sort_order === $b->sort_order ) {  
            return 0;  
        } elseif ( $a->sort_order > $b->sort_order ) {
            return -1;
        }
        return 1;  
    }  
    usort( $users, 'wpse98580_sort_order' );
    
    /* Iterate over the sorted array */
    

    For reference, see usort.

  3. There is an action that should do this. It doesn’t seem to be very documented but pre_user_query does show up in source.

    function alter_user_search($qry) {
      global $wpdb;
      $key = $qry->get('meta_key');
      if (isset($key)) {
    //    $qry->query_orderby = preg_replace('/ORDER BY (.*) (ASC|DESC)/',"ORDER BY {$wpdb->usermeta}.meta_value ".$qry->get('order') ,$qry->query_orderby);
        $qry->query_orderby = preg_replace('/ORDER BY (.*) (ASC|DESC)/',"ORDER BY CAST({$wpdb->usermeta}.meta_value AS UNSIGNED) ".$qry->get('order') ,$qry->query_orderby);
      }
    //   var_dump($qry); die;
    }
    add_action( 'pre_user_query', 'alter_user_search' );
    

    That will sort the query on the meta key if the key is set, which is probably too aggressive but you can add more conditions. This is minimally tested with values in my database but it does appear to work (fairly sure). There are two versions of that query. The second will CAST your meta_value to an INTEGER so you should get decent numeric sorting.

    Barely tested. Possibly buggy. Caveat emptor. No refunds.

Comments are closed.