craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 56 forks source link

Fixed order based on slug #190

Closed thunder87 closed 1 month ago

thunder87 commented 1 month ago

Description

Unable to order entries based on the order the slugs are received.

Steps to reproduce

  1. Copy below entries.json and view the entries.json output in the browser.
  2. Order will be the same regardless of how the slugs are ordered.

Additional info

return [
    'endpoints' => [
        'entries.json' => function() {
            $slugs = Craft::$app->getRequest()->getRequiredQueryParam('slugs');

              return [
                  'elementType' => Entry::class,
                  'criteria' => [
                      'slug' => $slugs
                  ],
                  'transformer' => function (Entry $entry) {
                      return [
                          "title" => $entry->title,
                      ];
                  },
              ];
        }
    ]
]
i-just commented 1 month ago

Hi, thanks for reaching out! This is expected behaviour.

By default, entries are ordered by postDate DESC, elements.id DESC. For your entries endpoint, you can provide your own order via criteria > orderBy.

There’s also the fixedOrder option, which might be helpful if you’re querying by ids: https://craftcms.com/docs/4.x/entries.html#fixedorder.

I hope this helps!