• Hi all

    I need to order the results of this query by multiple meta key values. Firstly by wpum_total_fb_reviews and then by wpum_curr_fb_rating. In the near future, I’ll need to add a third orderby meta value as well. I can get the orderby to work with just one meta key value, but not by any more. I have tried the recently introduced orderby array function, but can’t get that to work at all. The results still display, but in no desired order. Can anyone help me here?

    $args = array(
    	'meta_query' => array(
    		'relation' => 'AND',
    			array(
    				'key'     => 'wpum_select_cat',
    				'value'   => $cat,
    				'compare' => 'LIKE',
    			),
    			array(
    				'key'     => 'wpum_metro_region',
    				'value'   => $loc,
    				'compare' => 'LIKE',
    			),
    			array(
    				'key'     => 'wpum_curr_fb_rating',
    				'value'   => '4.0',
    				'type'    => 'numeric',
    				'compare' => '>=',
    			),
    			array(
    				'key'     => 'wpum_total_fb_reviews',
    				'value'   => '4',
    				'type'    => 'numeric',
    				'compare' => '>=',
    			)
    	),
    'orderby' => 'meta_value_num',
    'order'   => 'DESC',
    'meta_key' => 'wpum_total_fb_reviews',
     );
     
    $user_query = new WP_User_Query( $args );

    Thank you in advance.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Yeah, you’ll need to work out the proper way to provide an orderby array argument. With a proper array argument, do not use the meta_key argument, it may confuse the proper SQL construction. Defining the the key in meta_query will be enough. If you have differing order arguments for each meta key, do not use the order argument either, the order direction can be specified by key in the orderby array.

    As it turns out after digging deep into source code, the proper orderby array configuration is FAR from intuitive. I recently wrote up an explanatory user note in the developer code reference, but it probably will not appear soon enough for you to see it, so here it is:

    The specification for providing meta_query keys as orderby parameters is confusing at best. The docs say to use “fields as keys” and an accepted value is “an array key of $meta_query“. In fact values must be the array key from the array returned by $WP_Meta_Query::get_clauses(). You could get this array by instantiating WP_Meta_Query with your meta_query arguments, then calling WP_Meta_Query::get_sql('user','wp_users','ID'), (provide your correct table name for wp_users) then calling $WP_Meta_Query::get_clauses(). The array keys in the returned array are the correct keys to assign to the orderby argument.

    A perhaps easier way to get the proper keys is to instantiate WP_User_Query with your meta_query arguments, then examining the SQL query in WP_User_Query::request. In particular the WHERE portion related to user meta. The keys are the table aliases used in the SQL query. The first meta_query argument’s key is always the table name, usually wp_usermeta unless you specified a different prefix in wp-config.php. Additional meta_query arguments are assigned aliases like mt1, mt2, etc. The related meta key name occurs to the right of the = sign in each case. Thus your orderby argument might be array('mt1', 'wp_usermeta',) or if you need differing order arguments, array('mt1'=>'ASC', 'wp_usermeta'=>'DESC',)

    For reference, the user_meta part of the SQL WHERE clause may look something like this:
    (( wp_usermeta.meta_key = 'foo' AND wp_usermeta.meta_value LIKE '%bar%' )
    AND
    ( mt1.meta_key = 'sna' AND mt1.meta_value LIKE '%fu%' ))

Viewing 1 replies (of 1 total)
  • The topic ‘Implemeting orderby array with numeric meta values’ is closed to new replies.