Jump to content

filter repeater enries by date


Christoph
 Share

Recommended Posts

Hi there, 

I have a little problem with a repeater that I'm using for events, containing three fields:

start_date, end_date, description

On the homepage, I want to display the upcoming two events.
So I have to filter out past events, sort by the dagte and set a limit for the output
I thought I could do it with something like the following code, but there's no output:

$nextevents = $pages->get("template=events")->events->find("end_date>today, sort=end_date, limit=2");

foreach($events as $event) : ?>
<li class="future-event"><?= $event->end_date ?><span><?= $event->description ?></span></li>

<?php endforeach ?>

the template of the repeater is called "events" and the repeater field's name is also "events"

I guess it can't be that difficult, but after trying different things and looking through the docs, I still have no idea how to solve it.
 
Would be great if someone can help!

Thanks

Link to comment
Share on other sites

With $pages->get("template=events") you only get one page. You need to use $pages->find("template=events") to get all event pages.

Try

$nextevents = $pages->find("template=events")->events->find("end_date>today, sort=end_date, limit=2");

Not sure though if this works. You might need to loop through your events and inside the loop remove those that don' match required dates

$nextevents = $pages->find("template=events, sort=end_date");
foreach($nextevents as $e) {
  if ($e->getUnformatted(events->end_date) < time()) $nextevents->remove($e);
}
// get first 2 events with slice, see https://processwire.com/api/arrays/
nextevents = $nextevents->slice(0, 2);

Link to comment
Share on other sites

Thanks for your quick reply!

Find doesn't work in this context and produces an error:

Error: Call to a member function find() on a non-object

As I only need one page (the one where the repeater is included), I think "get" is ok here.

Your second idea sounds promising, but unfortunately, the code doesn't work either.

Anyway, thanks for your support!

Link to comment
Share on other sites

Your version is not working because the runtime selector currently does not support parsing "today" to it's timestamp like the db selector engine does.

$pages->find("template=repeater_events, end_date>today, sort=end_date, limit=2");
$pages->get("template=events")->events->find("end_date>" . time() . ", sort=end_date, limit=2");
  • Like 2
Link to comment
Share on other sites

It's not about pages or repeaters – which is irrelevant – but about the way you handle them. For the most part any method you call directly on the $pages object will result in a database query, whereas all the other find() or get() calls are only searching pages, which are already in memory. That's the differentiating factor.

  • Like 1
Link to comment
Share on other sites

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
 Share

×
×
  • Create New...