AEM query builder to fetch the cq:Pages under a path based on some tags in descending order based on multiple value | Community
Skip to main content
Level 2
December 9, 2024
Solved

AEM query builder to fetch the cq:Pages under a path based on some tags in descending order based on multiple value

  • December 9, 2024
  • 3 replies
  • 1539 views

I want to write a query in AEM query builder to fetch the cq:Pages under a path based on some tags in descending order based on multiple value.

I am able to write the query but the sorting part is little tricky. We have to sort the result primarily on publishedDate. if it is not there then use the lastmodified date. 

Only the sorting part I'm struggling. Please help 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

@linujo 

 

Please try orderby

Allows the results to be sorted. If ordering by multiple properties is required, this predicate must be added multiple times using the number prefix, such as 1_orderby=first, 2_oderby=second.

Properties

  • orderby

    Either JCR property name indicated by a leading @, for example, @jcr:lastModified or @jcr:content/jcr:title, or another predicate in the query, for example, 2_property, on which to sort.

  • sort

    Sort direction, either " desc" for descending or " asc" for ascending (default).

  • case

    If set to ignore, it makes sorting case insensitive, meaning “a” comes before “B”; if empty or left out, sorting is case-sensitive, meaning “B” comes before “a”

 

If OOTB doesn't give required result, create a custom Query Predicate. A sample code of similar usecase is available on this thread https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sortby-two-values-at-a-time/td-p/650221

3 replies

Community Advisor
December 9, 2024

Hi @linujo 

Please find the below query for your requirement.

 

type=cq:page
path=/content/path
orderby=@jcr:content/cq:lastReplicated
orderby.sort=desc
p.limit=-1

 

Hope this helps!

LinuJoAuthor
Level 2
December 9, 2024

Thanks for the answer, but this is not what I'm looking for. This is purely based on 1 property the sorting is happening, I want to use something like if there is publication date property is available then use it otherwise use the last modified date. Both are date properties.

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
December 9, 2024

@linujo 

 

Please try orderby

Allows the results to be sorted. If ordering by multiple properties is required, this predicate must be added multiple times using the number prefix, such as 1_orderby=first, 2_oderby=second.

Properties

  • orderby

    Either JCR property name indicated by a leading @, for example, @jcr:lastModified or @jcr:content/jcr:title, or another predicate in the query, for example, 2_property, on which to sort.

  • sort

    Sort direction, either " desc" for descending or " asc" for ascending (default).

  • case

    If set to ignore, it makes sorting case insensitive, meaning “a” comes before “B”; if empty or left out, sorting is case-sensitive, meaning “B” comes before “a”

 

If OOTB doesn't give required result, create a custom Query Predicate. A sample code of similar usecase is available on this thread https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sortby-two-values-at-a-time/td-p/650221

Aanchal Sikka
kapil_rajoria
Community Advisor
Community Advisor
December 9, 2024

Hi @linujo 
Please try:
path=/content/your-path
type=cq:Page
property=jcr:content/cq:tags
property.value=tag1,tag2
orderby=@jcr:content/cq:lastReplicated
orderby.sort=desc
orderby.secondary=@jcr:content/cq:lastModified
orderby.secondary.sort=desc

LinuJoAuthor
Level 2
December 9, 2024

Already tried this, but not giving the desired output. 

kapil_rajoria
Community Advisor
Community Advisor
December 16, 2024

Hi @linujo 
What problem you are facing using the above?