Support

Account

Home Forums ACF PRO Sorting relationship field in admin

Solved

Sorting relationship field in admin

  • Before I submit an official support ticket on this I thought I would check with the community and see if anyone has tried this before. I often use a relationship field to choose a series of related or featured posts. Since I’m using the ‘post’ post_type, I think it would be most logical for the user to be choosing from a chronological list of posts rather than an alphabetical list.

    Does anyone know if there’s a way to filter the relationship field on the admin side to sort by date rather than title?

  • There is a filter, it can be used in some cases but the results can be broken in other cases. The only thing you can do is try it and see if it works.

    http://www.advancedcustomfields.com/resources/acf-fields-relationship-query/

    I know that if the relationship is for a hierarchical post type, changing the orderby value breaks the results because ACF groups them by parent. There may be other issues that I’m not aware of as well.

  • That was the trick! Thanks so much, I had no idea this filter existed and it was exactly what I needed. Cheers.

  • There are other filters that are not documented at all, sometimes you need to dig around in the ACF code to find them all, and some of them don’t always work, which may be part of the reason they aren’t all documented.

  • What was the trick? I can’t figure out how to make newest posts show up first, instead of the default alphabetical sorting. Even in that link. Can anyone help?

  • Nevermind, I’m dumb. This was what I ended up doing:

    
    function my_relationship_query( $args, $field, $post_id ) {
    	
        // order by newest posts first
        $args['orderby'] = 'date';
        $args['order'] = 'DESC';
    	
        // return
        return $args;
        
    }
    
    // filter for every field
    add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Sorting relationship field in admin’ is closed to new replies.