2

I have created a search results page to display matching entries to a search term, ordered by the most recent.

The set-up works well for all entries which are not part of a structure. Results of entries within a structure appear at the end of the search results together and in reverse date order. Could this be that structure sections are searched separately from the rest of the site? The results are displaying, just not how they should.

I am working with legacy code and my knowledge of this feature is very limited.

Is anyone able to point me in the right direction for including entries from structures within the rest of the search results, and in date order?

Thank you!

{% set searchQuery = craft.app.request.getParam('q') %}
{% set entries = craft.entries()
.search(searchQuery)
.all() %}
5
  • Welcome to the Craft CMS Stack Exchange, @michellelclement! Is that example the actual element query you’re using? I’m not seeing date-ordering or specific sort methods.
    – Matt Stein
    Commented Sep 21, 2022 at 17:11
  • Hi Matt, Thanks for your reply! Yes, that is the actual query. The results are automatically sorted by the most recent entries first. However, do you think I need to add the specific sorting method for the structure elements to be included in the same result ordering? Commented Sep 26, 2022 at 6:54
  • 1
    I don’t know if you should have to specify the sort order, I’m just wondering if it makes any difference. Even if not, detailing whatever you’ve tried could be useful context.
    – Matt Stein
    Commented Sep 26, 2022 at 16:48
  • It appears you do! The results were displayed in date order, however, it wasn't sorting ALL the results into that order. Commented Oct 4, 2022 at 7:02
  • Glad you figured it out! 🎉
    – Matt Stein
    Commented Oct 4, 2022 at 16:00

1 Answer 1

2

Update the query with the orderBy as well.

{% set entries = craft.entries()
.search(searchQuery)
.orderBy('postDate desc')
.all() %}
1
  • Thank you so much! I can confirm this worked. Commented Oct 4, 2022 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.