Support

Account

Home Forums Backend Issues (wp-admin) Reorder ACF Groups (per page basis)

Solving

Reorder ACF Groups (per page basis)

  • Hi there, I was wondering if it is possible to set different Field Groups ordering per page.

    For example, we’ve got a “Video” Field Group – on homepage we want it to be the third Field Group, but on “About us” page we want this field to be the first one (or tenth) field group.

    If we change the order of this field on homepage (using drag and drop in the editor), we also edit the ordering of the field groups on about page.

    Is it possible to set a totally different (separate) ordering per page basis?

    PS Apologies for my English

  • There isn’t any way to do this per page/post of the same post type. It is WP that is remembering and ordering the groups when you move them and it does this on a per/screen basis (post type, taxonomy, etc.). WP stores this value in the _usermeta table with meta_key of meta-box-order_{$screen}. There are no filter hooks available that would allow altering this order directly.

    There might be a way to modify this value.

    WP calls this to get the option

    
    $sorted = get_user_option( "meta-box-order_$page" );
    

    There is a filter in get_user_option()

    
    return apply_filters( "get_user_option_{$option}", $result, $option, $user );
    

    It should be possible to alter the order on the “page” post type with a filter

    
    add_filter('get_user_option_meta-box-order_page', 'YourFunctionName', 10, 3)
    

    Exactly how you you would alter the returned value I couldn’t say without actually building something and testing it.

  • You can make it kind of work.

    As you said the problem is that WP saves the order of boxes per post type so when
    used on ‘page’ type post you will have problems when you have different set of fields for the homepage and a generic page.

    On a homepage type of page (post) we can set order like:
    header, homepage fields
    but then when we go to a generic page where we have:
    header, generic fields

    So when the order is set for the homepage by the user like: header, homepage fields, WP saves this to the metafield meta-box-order_page and when you go to edit a generic page, WP tries to sort the meta boxes in order: header, homepage fields.

    Unfortunately there’s no ‘homepage fields’ group here so it only takes ‘header’ to ‘sorted’ set and the ‘homepage fields’ is left in the group where it was at first and that is ‘high’ priority group, then WP writes the metaboxes starting with ‘high’ then ‘sorted’ groups – this way on generic page we get ‘generic fields’, ‘headers’ and on a homepage ‘hompeage fields’ , ‘headers’.

    So the only way I see here is to disable all the sorting for ACF groups, thus leaving them in the initial ‘high’ priority group and relay on the order of the ACF groups in the define ACF groups admin panel section.

    As for disabling this I used the mentioned filter:

    add_filter('get_user_option_meta-box-order_page', 'removeAcfGroupsFromUserSort', 10, 3);
    
    function removeAcfGroupsFromUserSort($result, $option, $user)
        {
          if (empty($result) || empty($result['normal'])) return $result;
    
          $metaboxIds = explode(',', $result['normal']);
    
          $metaboxIdsOut = array_filter($metaboxIds, function($metaboxId) {
            return (!strstr($metaboxId, 'acf-group'));
          });
    
          $result['normal'] = implode(',', $metaboxIdsOut);
    
          return $result;
        }
  • It could be possible to set this on a per page basis. I do not have any code, just a vague outline.

    The main issue that I have found it that the meta box order is saved with AJAX as the user moves the boxes around. If I had to do this I would.

    This is just for pages, it would need to be duplicated for other edit page box orders.

    1. Create an acf/save_post action
    2. Get the user option
      
      $user = get_current_user_id();
      $order = get_user_option("meta-box-order_page", $user)
      
    3. Create a new option that included the post ID
      
      update_user_meta($user, "meta-box-order_page-$post_id", $order);
      
    4. Add a filter on get_user_option_meta-box-order_page like @piotku posted
      
      add_filter('get_user_option_meta-box-order_page', 'meta_box_order_by_page', 10, 3);
      
    5. In that filter get and return the users meta data saved for the current post ID

    The main issue that I see with this is getting the current post ID because I don’t know if that information will be available or how it would be available. Potentially global $post might be set.

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

You must be logged in to reply to this topic.