• nitrospectide

    (@nitrospectide)


    Is there any way to switch the Relationship field UI so that items get added to the top of the list, and not the bottom?

Viewing 3 replies - 1 through 3 (of 3 total)
  • noahwpengine

    (@noahwpengine)

    Hi @nitrospectide

    Yes.
    You could attach a filter to the relationship field that alters the query parameters so the posts return in the order you define them. This is the filter I am referring to: https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/

    You could supply it with new order and orderby parameters – https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

    Something like below:

    add_filter('acf/fields/relationship/query', 'my_acf_fields_relationship_query', 10, 3);
    function my_acf_fields_relationship_query( $args, $field, $post_id ) {

    //Sorts from newest (latest) to oldest.
    //So new posts at the top, older at the bottom.
    $args['order'] = 'DESC';

    //Order by their publication date / creation date.
    $args['orderby'] = 'date';

    return $args;
    }

    The above is example code and would act on all relationship field on the site.
    We are assuming you can alter it to fit your specific use case.

    Modify and try it. Then let us know if it works out for you.
    Noah 🙂

    Thread Starter nitrospectide

    (@nitrospectide)

    I don’t fully understand what that does, but I think it’s different than what I’m asking for.

    My use case is: when I am in the admin on a post, and I want to add items to the relationship field, it’s easy to find the item I want to add, but when I click the item in the left hand column to add it to the right hand column, it always adds it at the bottom. I then have to scroll to the bottom on the right-hand list, and drag that item all the way to the top, because we tend to want to place our additions closer to the top of the list than the bottom. And the list is kind of long, so the drag/drop is a bit clunky.

    noahwpengine

    (@noahwpengine)

    Hi @nitrospectide,

    Okay understood.
    There isn’t a setting for this an you would probably need to set up custom javascript to reverse the order of the choices list after a new item is added to the list.

    Give me some time and I will try and cook something up.
    Noah.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.