jgunderson
(Jansen Gunderson)
March 7, 2023, 7:12pm
1
Orderings does not seem to be working in Nextjs when using getStaticProps
and getAllByType
.
We can correctly order the response when using getBySomeTags
, however.
The following code does not correctly order by a date field:
const newsItems = await client.getAllByType('news', {
limit: 3,
orderings: [
{
field: 'my.newsItems.data.first_publication_date',
direction: 'desc'
}
]
});
When we use getBySomeTags
, the response is correctly ordered:
const newsItems = await client.getBySomeTags(['News'], {
limit: 3,
orderings: [
{
field: 'my.newsItems.published_date',
direction: 'desc'
}
]
});
Pau
March 7, 2023, 7:40pm
2
What do you get in the API response @jgunderson ?
jgunderson
(Jansen Gunderson)
March 7, 2023, 9:21pm
4
Thank you @Pau ,
here is the response:
{
id: 'Y6spKhcAAFQCpJid',
uid: 'employee-generation-study-eps',
url: '/news/employee-generation-study-eps',
type: 'news',
href: 'https://teton-valley-housing.cdn.prismic.io/api/v2/documents/search?ref=ZAeIAxAAACkAu1e9&q=%5B%5B%3Ad+%3D+at%28document.id%2C+%22Y6spKhcAAFQCpJid%22%29+%5D%5D',
tags: [ 'News' ],
first_publication_date: '2022-12-27T17:19:42+0000',
last_publication_date: '2023-03-07T18:52:27+0000',
slugs: [
'economic--planning-systems-hired-for-employee-generation-study',
'bring-to-the-table-win-win-survival-strategies'
],
linked_documents: [],
lang: 'en-us',
alternate_languages: [],
data: {
title: [Array],
excerpt: [Array],
published_date: '2023-02-23',
featured_image: [Object],
article: [Array]
}
}
]
I'm trying to order by published_date
.
Pau
March 8, 2023, 5:38pm
5
Can you tell me what would the expected date of the document?
What's the URL of your repository? I'd like to test it out (you can send it to me via dm if you prefer).
jgunderson
(Jansen Gunderson)
March 8, 2023, 9:41pm
6
The expected date would be 2023-02-23 for that particular document.
It looks like your profile is hidden so I can't dm you.
Thanks!
Pau
March 9, 2023, 5:04pm
7
I sent you a message. You can see it in your inbox now.
Pau
March 11, 2023, 1:15am
8
I noticed you're trying to query the last publication date from within the data node. You need to write the query like this instead because this is a metadata field:
{
orderings: {
field: 'document.first_publication_date',
},
}
jgunderson
(Jansen Gunderson)
March 12, 2023, 3:39pm
9
Hi @Pau ,
thank you for your response. I'm not trying to query the metadata field, but rather a custom date field called published_date
.
Thanks,
Jansen
Pau
March 13, 2023, 5:06pm
10
I understand. The query has a syntax error. If your Custom type is named newsItems
. You need to use the my.[custom-type].[field]
path. In this case, it would be my.newsItems.published_date
.
const newsItems = await client.getAllByType('news', {
limit: 3,
orderings: [
{
field: 'my.newsItems.published_date',
direction: 'desc'
}
]
});
Check out the REST API documentation to learn more about date predicates
The Prismic Rest API is an extremely fast, flexible, and powerful engine for your content. On this page, you will learn how to query the Rest API, including how to use Predicates, query options, and GraphQuery.