Support

Account

Home Forums General Issues Change the order in relationship picker

Solving

Change the order in relationship picker

  • Why doesn’t this work to change the order in a relationship picker (seen while editing post)

    add_filter('acf/fields/relationship/query/key={field_62585c13349c2}', 'my_acf_fields_relationship_query', 10, 3);
    function my_acf_fields_relationship_query( $args, $field, $post_id ) {
    
        // change the order in the relaionship picker
        $args['orderby'] = 'date';
    
        return $args;
    }
  • ‘orderby’ is set by ACF after the acf/fields/relationship/query/ filter. The reason for this is that ACF groups the posts based on post type when posts can be selected from multiple post types. It also orders them by parent/child. There isn’t a filter in ACF that can be used to alter this.

    To accomplish this you would have to add a pre_get_posts action inside of your filter that would be used by the query that ACF eventually runs. Altering the order at this point will break the relationship field display in some cases, especially when posts can be selected from more than one post type.

  • Thanks much for your reply. Is there an easy way to test if this would break the relationship field display in my case?

  • The only way is to try it and find out.

  • Hi there. I found this via search, same exact question. Understanding that we need to test this out, do you have sample code or a similar question that has code?

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

You must be logged in to reply to this topic.