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


Default sort order when adding images

  • My clients like to name his images “image_001”, “image_002” etc… so that he can just drag and drop them into wordpress and not worry about ordering images later.

    Problem is that wordpress is ordering media by upload date, and not by alphabet.

    There is bulk action “Sort by title” in acf gallery but that requires manual intervention.

    Is there a way to trigger “Sort by title” bulk action when adding new images to gallery field?

  • Anyone? Is this possible via some ACF javascript api or something?

  • So is there any solution to this? I can’t find a clear answer. Yes or no?

  • 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
      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');
  • If you are talking about the way the order that the images are shown for selecting them you would want to alter the ajax query. See this topic and the topic that I linked to there. This would be done in a similar fashion

  • I don’t think we understood each other well. Gallery field has that bulk option dropdown that allows me to sort images in gallery field by title, reverse order etc…

    What I’d like to know is if there is some way to trigger that sort action when I’m adding new images into the gallery.

  • I guess the answer to that is it depends on how much work you want to do.

    You would need to add custom JavaScript. In this JS you would need to detect that the gallery has changed, as it happens I’ve been working doing just that for a project that I’m working on. I can give you some ideas for what’s involved, but I can give you details on the exact code to get it done.

    To trigger the change event you need to target all of the hidden fields in the gallery. For jQuery the selector looks like

    $('[data-key="field_589f92d2d394a"] input[type="hidden"]')

    where data-key is the field key of your gallery field.

    I can share some of my code with you, basically it is written in the same style as this JS from one of my dynamic field examples.

    My event is this

    'change [data-key="field_589f92d2d394a"] input[type="hidden"]': '_get_set_change_gallery',

    and this is the function I have so far

    _get_set_change_gallery: function(e) {
      // we use a timeout here because if several images are added all at once
      // then this function will be triggered several times
      // we can avoid runing this multiple times by using a timeout
      // we also want to set a timeout like with images to give them time to load
      if (window.gallery_change_timeout) {
      window.gallery_change_timeout = setTimeout(function() {
        window.gallery_change_timeout = null;
        // ****************************************
        // code to do what you want here
        // ****************************************
      }, 200);

    Your next step for what you wan to do you need to put where it says code to do what you want here.

    What you want to do is find the sort select field, change the value of that field and then trigger the change event on that field.

    Not sure if that helps you get to where you want to go.

  • @kuhada


    Gallery field has that bulk option dropdown that allows me to sort images in gallery field by title, reverse order etc…

    I don’t see this possibility when editing a Photo Gallery field when creating a custom post having such a field attached. Could you please elaborate a little?

    Thanks in advance.


  • Thanks very much.

    However, I don’t have this option when adding image from the Media Library. It seems that Real Media Library is hiding it somehow (I’m using RML, Elementor and the Astra theme).

    I have code in functions.php that is already doing a few things with the $image array. I could possibly do the sort there. I cannot find accurate information about which fields this array contains. Has it a filename field ?

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Default sort order when adding images’ is closed to new replies.