Jump to content

Recommended Posts

Posted

Hello,

I'm currently building an events website where one event can be in different locations. Each location can have it's own date. I therefore have added a repeater field called `event_locations` to an event where we can add more information about the location.

The `event_locations` repeater contains the following fields:

  • location (page reference)
  • event_date (datetime field)
  • event_price (text field)
  • event_link (url field)

On the event detail page I would like to show the different locations and sort them by date and city. Events in the past can be removed from the page.

I normally check the date with `event_date>=today` which works whenever I loop through all pages and filter by template. However in this case I would like to filter through the repeater field which doesn't give the same results for some reason.

This works (but displays all future event_locations):

$pages->find('event_date>=today, sort=event_date, sort=location.location_city')

This doesn't work (this give no results at all):

$page->event_locations->find('event_date>=today, sort=event_date, sort=location.location_city')

Can't the event_date be used in this case? Or is there a different way to get only events in the future for this specific event?

Posted

Did you try this kind of selector? There's no loop, you directly get the repeater item. The part with "owner" is not mandatory, it depends if you want to do the search on a specific page or not.

 

Posted

 The difference between the two snippets you posted is that $pages->find() will make a database query and $page->event_locations->find() filters the repeater items already in memory (a PageArray). Confusingly, there are some features that are only available in one or the other. My guess is probably the “today” condition doesn’t work with in-memory selectors, because it doesn’t know you’re comparing dates at that point. So under the hood it’ll be something like 1726956531 >= 'today' (or maybe '09/22/2024 00:08' >= 'today' due to output formatting, I’m not sure).

As @da² suggested, try this:

$pages->find('template=repeater_event_locations, event_date>=today, include=all, event_locations.owner.id=' . $page->id . ', sort=event_date, sort=location.location_city')

That should get all repeater items from the database which belong to $page.

If you want to use $page->event_locations instead, I would suggest just foreaching them and skipping the ones you don’t want. You’re going to foreach them anyway, so you might as well do filtering and output in a single loop. But the other option should be more performant, because you’re never loading the ones you don’t need.

  • Like 1
Posted

@Jan Romero and @da², thanks for your help! I just tried it again with the selector from Jan and it gives the correct results. I didn't correctly understand the query from the original post for my situation but this helped a lot. Thanks!

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...