Sorting relationship items

default discord avatar
6 months ago

Hey guys, I have problem when I want to swap field component of relationship, and use


hook to update the field. To add the items it works, but somehow I can't change the order of items.

I use setValue with an array, i.e.

initialValue ->

['a', 'b', 'c']


setValue(['c', 'b', 'a'])

then refresh page still

['a', 'b', 'c']

it is works when i add item, i.e

['a', 'b', 'c', 'd']

Is there another way to set item's order?

  • discord user avatar
    Payload Team
    6 months ago

    hi @rrums - where are you getting the array of data from? as you can sort on requests
  • default discord avatar
    6 months ago

    not to querying, but to store data (array of IDs)

    because i swapped the field component, but i can't change order of the IDs

    like this -> i log the


    , it has correct arrays, the order is set as I drag the item. But it wasn't stored with correct order.

    FYI, the field on collection

      name: 'items',
      type: 'relationship',
      relationTo: 'menuItems',
      hasMany: true,
      required: true,
      admin: {
        components: {
          Field: MenuSelect // custom component
        isSortable: true,
  • default discord avatar
    6 months ago

    Curious, what does the network panel look like for this request?

  • default discord avatar
    6 months ago

    ouch found the problem, because I fetch not from initialValue. My bad.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!


Connect with the Payload Community on Discord



Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.