Support

Account

Home Forums ACF PRO Sorting relationship field in admin

Solved

Sorting relationship field in admin

    • Dalton

    • October 30, 2015 at 4:52 am

    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.

    • Dalton

    • October 30, 2015 at 6:53 am

    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.

    • Corey

    • March 31, 2018 at 9:54 am

    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?

    • Corey

    • March 31, 2018 at 10:22 am

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.