Support

Account

Home Forums Add-ons Gallery Field Default sort order when adding images Reply To: Default sort order when adding images

  • This is more of a WP question than an ACF question. ACF does not have the ability built into it to alter the sort order. In ACF5 The images are returned in the order they appear in the gallery, this can be changed by dragging the images around in the gallery.

    What you need to do is to create a pre_get_posts filter. There are a lot of turorials available on the subject.

    your filter might look something like this (please note that I have not tested this and it might require testing and changes)

    
    // this would go in function.php
    function acf_order_gallery_by_name($query) {
      // do some checking to make sure we want to change order
      if (is_admin()) {
        // not in admin
        return
      }
      if (isset($query->query_vars['post_type']) &&
          $query->query_vars['post_type'] == 'addtachment') {
        $query->set('orderby', 'title')
      }
    }
    

    I would suggest that you also only run this filter when you need it like this so it does not effect other attachment queries.

    
    // add the filter
    add_action('pre_get_posts', 'acf_order_gallery_by_name');
    // get gallery field
    $gallery = get_field('my_gallery');
    // remove filter 
    remove_filter('pre_get_posts', 'acf_order_gallery_by_name');