• I am learning WordPress meta query. I am trying to fetch users with asc order of last_name & first_name. My code is like below.

    
    if($_POST['orga_id']) {
    		$users_query = array(
    			'meta_query' => array(
    				'relation' => 'AND',
    				array(
    					'relation' => 'OR',
    					array(
    						'key'     => 'first_name',
    						'value'   => $_POST['search'],
    						'compare' => 'LIKE'
    					),
    					array(
    						'key' 		=> 'last_name',
    						'value' 	=> $_POST['search'],
    						'compare' 	=> 'LIKE',
    					),
    				),
    				array(
    					'key' 		=> 'organisation',
    					'value' 	=> $_POST['orga_id'],
    					'compare' 	=> 'LIKE',
    				),
    				array(
    					'key' 		=> 'available',
    					'value' 	=>  1,
    					'compare' 	=> '=',
    				),			
    			)
    		);
    	} else {
    		$users_query = array(
    			'meta_query' => array(
    				'relation' => 'AND',
    				array(
    					'relation' => 'OR',
    					array(
    						'key'     => 'first_name',
    						'value'   => $_POST['search'],
    						'compare' => 'LIKE'
    					),
    					array(
    						'key' 		=> 'last_name',
    						'value' 	=> $_POST['search'],
    						'compare' 	=> 'LIKE',
    					),
    				),
    				array(
    					'key' 		=> 'available',
    					'value' 	=>  1,
    					'compare' 	=> '=',
    				),			
    			)
    		);
    	}
    

    How can I fetch users with asc order of last_name & first_name ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • <?php
    $args = array(
        'orderby' => 'meta_value',
        'meta_key' => 'last_name',
        'order'   => 'ASC'
    );
    $users = get_users( $args );
    echo '<ul>';
    foreach ( $users as $user ) {
        echo '<li>' . $user->first_name .' '. $user->last_name . '</li>';
    }
    echo '</ul>';
    ?>

    Something like that?

    Thread Starter mabufoysal

    (@mabufoysal)

    Thanks @tugbucket . I used like this.

    
    `if($_POST['orga_id']) {
    		$users_query = array(
    			'meta_query' => array(
    				'relation' => 'AND',
    				array(
    					'relation' => 'OR',
    					array(
    						'key'     => 'first_name',
    						'value'   => $_POST['search'],
    						'compare' => 'LIKE'
    					),
    					array(
    						'key' 		=> 'last_name',
    						'value' 	=> $_POST['search'],
    						'compare' 	=> 'LIKE',
    					),
    				),
    				array(
    					'key' 		=> 'organisation',
    					'value' 	=> $_POST['orga_id'],
    					'compare' 	=> 'LIKE',
    				),
    				array(
    					'key' 		=> 'available',
    					'value' 	=>  1,
    					'compare' 	=> '=',
    				),
    				array(
    					'orderby' => 'meta_value',
    					'meta_key' => 'last_name',
    					'order' => 'ASC',
    				),
    				array(
    					'orderby' => 'meta_value',
    					'meta_key' => 'first_name',
    					'order' => 'ASC',
    				),
    			)
    		);
    	} else {
    		$users_query = array(
    			'meta_query' => array(
    				'relation' => 'AND',
    				array(
    					'relation' => 'OR',
    					array(
    						'key'     => 'first_name',
    						'value'   => $_POST['search'],
    						'compare' => 'LIKE'
    					),
    					array(
    						'key' 		=> 'last_name',
    						'value' 	=> $_POST['search'],
    						'compare' 	=> 'LIKE',
    					),
    				),
    				array(
    					'key' 		=> 'available',
    					'value' 	=>  1,
    					'compare' 	=> '=',
    				),
    				array(
    					'orderby' => 'meta_value',
    					'meta_key' => 'last_name',
    					'order' => 'ASC',
    				),
    				array(
    					'orderby' => 'meta_value',
    					'meta_key' => 'first_name',
    					'order' => 'ASC',
    				),			
    			)
    		);
    	}
    

    But I am not getting last_name & first_name as output.

    @mabufoysal Looks like you have a lot going on in that code so it’s hard for to test. What does print_r($users_query); result in?

    Here is an example sort by multiple meta keys

    $args = array(
        'meta_query' => array(
            'relation' => 'AND',
            'query_one' => array(
                'key' => 'key_one',
                'value' => 'value_one', // Optional
            ),
            'query_two' => array(
                'key' => 'key_two',
                'compare' => 'EXISTS', // Optional
            ), 
        ),
        'orderby' => array( 
            'query_one' => 'ASC',
            'query_two' => 'DESC',
        ),
    );
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to Ordering by user meta’ is closed to new replies.