php – WordPress WP_User_Query-ThrowExceptions

Exception or error:

How can I show user and order them by 2 meta fields? For example I have user query

$args = array(
        'role'                => 'specialist',
        'meta_key'            => 'proffession',
        'meta_value'          => $term->ID,
        'meta_compare'        => '=',
        'number'              => 20,
        'paged'               => $paged,
    );
$user_query = new WP_User_Query($args);

I want to sort this query by two meta_values, for example in PHP:

 if(get_usermeta( $user_id, $meta_key = 'be_first' )>time() AND get_usermeta( $user_id, $meta_key = 'pro_date' )>time())
    // show this users first  ordered by be_first DESC, ordered by pro_date
if($pro_date>time()){
    //show this users second ordered by pro_date desc
}
    //show all other users
How to solve:

You can try this… I don’t have your fields, so this is not tested. You can create a meta_query where your clauses are set to compare exists then use those to orderby.

$args = array(
        'role'              => 'specialist',
        'number'            => 20,
        'paged'             => $paged,
        'meta_query'        => array (
            'relation' => 'AND',
            array(
                'key'     => 'profession',
                'value'   => '$term->ID',
                'compare' => '=',
            ),
            'be_first_clause' => array(
                'key'     => 'be_first',
                'compare' => 'EXISTS',
            ),
            'pro_date_clause' => array(
                'key'     => 'pro_date',
                'compare' => 'EXISTS',
            ),
        ),
        'orderby' => array(
            'be_first_clause' => 'ASC',
            'pro_date_clause' => 'ASC'
        )
    );
$user_query = new WP_User_Query($args);

Leave a Reply

Your email address will not be published. Required fields are marked *