sort – sorting posts by acf select field

Question

I have a field in ACF called ‘main_color’ my goal is to sort first by main_color, then by average hue as only average hue was causing some issues.

    $color_direction = 'asc';
    $main_direction = 'asc';
    if($args['order'] == 'desc')
    {
      $main_direction = 'desc';
      $color_direction = 'desc';
      $lightness_direction = 'asc';
    }
    $args['meta_query'] = array(
      'relation' => 'AND',
      'main_color' => array(
        'key' => 'main_color',
        'compare' => 'exists'
      ),
      'color_order' => array(
        'key' => 'avg_hue',
        'compare' => 'exists',
        'type' => 'numeric'
      ),
      'lightness_order' => array(
        'key' => 'avg_lightness',
        'compare' => 'exists',
        'type' => 'numeric'
      )
    );
    $args['orderby'] = array(
      'main_color' => $main_direction,
      'color_order' => $color_direction,
      'lightness_order' => $lightness_direction
    );

I’m only returning posts with a main_color set so I know its detecting that correctly but it is not ordering based off of the main_color field.

avg_hue is also a ACF field but it is a number instead of a select and works as expected. Is there anything particular about select ACF that needs to be set to get that type to work for sorting?

0
lathomas64 2 months 2022-09-25T12:41:32-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse