• Resolved shuangmiles

    (@shuangmiles)


    I’m looking to sort by three custom fields that are not part of the “OR” criteria in the meta_query and I haven’t found a good way to go about it, any suggestions? Here is my code below

    <?php $args = array (
                                          'post_type' => 'team',
                                          'posts_per_page' => -1,
                                          
                                          
                                          'meta_query'	=> array(
                                            'relation'		=> 'OR',
                                            array(
                                              'key'	 	=> 'department',
                                              'value'	  	=> $team,
                                              'compare' 	=> 'IN',
                                            ),
                                            array(
                                              'key'	  	=> 'second_department',
                                              'value'	  	=> $team,
                                              'compare' 	=> '=',
                                            )
    
                                            
                                         
                                          ),
                                          'orderby' => array('rank' => 'ASC', 'position' => 'ASC', 'last_name' => 'ASC' )
                                      );
    
                                      $team_query = new WP_Query( $args );
                                      $number = 0;
                                      ?>
    • This topic was modified 2 years, 6 months ago by t-p. Reason: Moved to Fixing WordPress from Developing with WordPress
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The outer ‘meta_query’ array needs to be made fully associative by assigning key names to each inner array element. Refer to the WP_Query doc page. Find the Order and Orderby section in the contents, follow the internal link. Scroll down to find the example ‘orderby’ with multiple ‘meta_key’s. (I cannot provide a direct deep link because the collapsed code examples mess up the scroll positioning of the browser. You wouldn’t land at the right position)

    If you don’t want a key used in selection criteria, but want to use it for ordering, only provide the “key” element in the inner array and nothing else. In the doc example, the “city_clause” element only affects selection in that the key needs to exist. You can even remove the 'compare' => 'EXISTS', element to not even require it exists, as you might want for a secondary ordering criteria.

    Thread Starter shuangmiles

    (@shuangmiles)

    Thanks I think I got it to work except how do I specify that ‘rank’ is a meta_val_num i noticed it some pages it’s reversed order and I think it’s reading the number as a string still?

    'meta_query'	=> array(
                                            'relation' => 'AND',
                                            'rank_key' => array(
                                              'key' => 'rank',
                                              'orderby'   => 'meta_value_num'
                                            ),
                                            'position_key' => array(
                                              'key' => 'position'
                                            ),
                                            'last_name_key' => array(
                                              'key' => 'last_name'
                                            ),
                                            array(
                                              'relation' => 'OR',
                                              array(
                                                  'key'	 	=> 'department',
                                                  'value'	  	=> $team,
                                                  'compare' 	=> 'IN',
                                                ),
                                                array(
                                                  'key'	  	=> 'second_department',
                                                  'value'	  	=> $team,
                                                  'compare' 	=> '=',
                                                )
                                            )
    
                                          'orderby' => array('rank_key' => 'ASC',  'last_name_key' => 'ASC' )
    Thread Starter shuangmiles

    (@shuangmiles)

    Actually nevermind I just found it, you basically put ‘type’ => ‘NUMERIC’ in the array with ‘rank’. Thanks for your help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Looking to sort by multiple custom fields in with wp_query’ is closed to new replies.