Sorting by 2 date fields on the same document type

Hello,
I am currently migrating from wordpress to prismic and after importing my documents, I have a custom field wp_publish_date for tracking older published dates as currently editing the inbuilt metadata field first_publication_date is not possible.

For fetching the list of documents I am using @prismicio/client - V6
I wish the list to be ordered by wp_publish_date if it exists or fallback to first_publication_date for newer documents. And all should be in descending order for the recent posts first.

const client = createClient(<Repo_Endpoint>);
client.getByType('post',{
    pageSize: 20,
    orderings: [
      {
        field: 'docment.first_publication_date',
        direction: 'desc',
      },
      {
        field: 'my.post.wp_publish_date',
        direction: 'desc',
      },
    ],
  })

I cannot fetch all and sort the array on frontend as there are 800+ records and it is not the most optimised solution to load 800 items to show 20.

Hi Amogh,

Welcome to the Prismic community.

If you are planning to display only the first 20 documents, then you would not need to fetch the 800+ papers and sort them in the frontend

Your query should generally work, but the only part that I'm not sure about is the fallback when the field wp_publish_date doesn't exist for the Prismic-created documents.

Also I've noticed that you have a typo in field: 'docment.first_publication_date', where should be field: 'document.first_publication_date',

So I will do some research about fallback, and it would be helpful if you could share with us your repository name (in a private message if necessary).

Hi @amogh.kajaria,
I apologize for the long delay (I was on vacation); so I have checked with the dev team, and here is the current behavior:

All documents with no date will be ordered together at the end for example:
if we have the documents : A,B,C,D
A: ({field1:undefiend} {filed2: 02/05/2022})
B: ({field1:03/04/2022}, {filed2: 03/05/2022})
C: ({field1:04/04/2022, {filed2: 04/05/2022})
D: ({field1:undefiend} {filed2: 06/05/2022})
will be sorted like B,C,A,D

Please let us know if you have any order ininquiries,
Fares

This actually means that you can't do the sorting from the Prismic API, and instead, it would be necessary to fetch all documents and sort them in the front end (or client for SSR).

And for that, I will create a feature request to allow a fallback of the ordering if the field doesn't exist.

Hi @Fares ,
Thank you for your inputs. Currently I was able to find a workaround.
I created a new boolean field with a default true value and made sure the migrated documents have the value of the field as false.
On Front-End while fetching the list, I am passing ordering in 3 levels

  1. New boolean field: So it separates the new and migrated documents
  2. Wordpress Date, so only the documents that have the field (migrated) are sorted by the field
  3. Prismic first creation date, which leaves out the new documents to be sorted by the prismic metadata field.

This approach seems to be working currently but it would be great to have the functionality added out of the box.
Thank you for the help.

Amogh.

1 Like

Nice workaround :grinning: , thanks for sharing that with the community