Jump to content
Christoph

filter repeater enries by date

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

Share this post


Link to post
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);

Share this post


Link to post
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!

Share this post


Link to post
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 1

Share this post


Link to post
Share on other sites

Thanks, LostKobraKai, 

your version works! 

Just to get it right:

Is "today" working because I'm searching within repeater fields?

Share this post


Link to post
Share on other sites

Yeah. The repeater field is returning an PageArray, which you're then searching. It's like $pages->find("some=pages")->find("now_search_on=runtime"). The latter one is less powerful/convenient than the first one.

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ICF Church
      Hi 👋
      Anyone else having this problem?
      Requirements:
      - Repeater (matrix & normal) with mutlilanguage fields (text, textarea…) 
      - Backend language set to something other than default (ie. German) 
      Reproduce:
      - Add a new repeater Item (ajax, I found no way to possible to disable it with matrix)

      (Notice how the default language tab is active instead of the backend language…)
      - Write something into the (default language) field
      - Try to save, if field is required, this will not work. If not required, then when reloading, the content will be inside the backend language field, instead of the default language field who was (presumably) active
      Analysis:
      When  loading  a new repeater element with ajax, the default langue tab is active, but the backend language inputfield is visible (with no visual indication). When writing into the field, it will populate the backend language. When manually clicking on the default language tab (which is already active), the field will switch to the actual default language field (which is [now] empty) (that can now be populated…)
      Also Notice, the labels of the elements to be added are in default language as well instead of the translated label (images instead of Bilder)…
      ProcessWire 3.0.148, Profields 0.0.5…
      Is it my system configuration, or does anyone else have the same issue? This is a screen recording of the problem:

      Screen Recording 2020-02-25 at 14.18.31.mov
    • By David Karich
      ProcessWire InputfieldRepeaterMatrixDuplicate
      Thanks to the great ProModule "RepeaterMatrix" I have the possibility to create complex repeater items. With it I have created a quite powerful page builder. Many different content modules, with many more possible design options. The RepeaterMatrix module supports the cloning of items, but only within the same page. Now I often have the case that very design-intensive pages and items are created. If you want to use a content module on a different page (e.g. in the same design), you have to rebuild each item manually every time.
      This module extends the commercial ProModule "RepeaterMatrix" by the function to duplicate repeater items from one page to another page. The condition is that the target field is the same matrix field from which the item is duplicated. This module is currently understood as proof of concept. There are a few limitations that need to be considered. The intention of the module is that this functionality is integrated into the core of RepeaterMatrix and does not require an extra module.
      Check out the screencast
      What the module can do
      Duplicate a repeater item from one page to another No matter how complex the item is Full support for file and image fields Multilingual support Support of Min and Max settings Live synchronization of clipboard between multiple browser tabs. Copy an item and simply switch the browser tab to the target page and you will immediately see the past button Support of multiple RepeaterMatrix fields on one page Configurable which roles and fields are excluded Duplicated items are automatically pasted to the end of the target field and set to hidden status so that changes are not directly published Automatic clipboard update when other items are picked Automatically removes old clipboard data if it is not pasted within 6 hours Delete clipboard itself by clicking the selected item again Benefit: unbelievably fast workflow and content replication What the module can't do
      Before an item can be duplicated in its current version, the source page must be saved. This means that if you make changes to an item and copy this, the old saved state will be duplicated Dynamic loading is currently not possible. Means no AJAX. When pasting, the target page is saved completely No support for nested repeater items. Currently only first level items can be duplicated. Means a repeater field in a repeater field cannot be duplicated. Workaround: simply duplicate the parent item Dynamic reloading and adding of repeater items cannot be registered. Several interfaces and events from the core are missing. The initialization occurs only once after the page load event Changelog
      1.0.4
      Bug fix: Various bug fixes and improvements in live synchronization Bug fix: Items are no longer inserted when the normal save button is clicked. Only when the past button is explicitly clicked Feature: Support of multiple repeater fields in one page Feature: Support of repeater Min/Max settings Feature: Configurable roles and fields Enhancement: Improved clipboard management Enhancement: Documentation improvement Enhancement: Corrected few typos #1 1.0.3
      Feature: Live synchronization Enhancement: Load the module only in the backend Enhancement: Documentation improvement 1.0.2
      Bug fix: Various bug fixes and improvements in JS functions Enhancement: Documentation improvement Enhancement: Corrected few typos 1.0.1
      Bug fix: Various bug fixes and improvements in the duplication process 1.0.0
      Initial release Support this module
      If this module is useful for you, I am very thankful for your small donation: Donate 5,- Euro (via PayPal – or an amount of your choice. Thank you!)
      Download this module
      > Github: https://github.com/FlipZoomMedia/InputfieldRepeaterMatrixDuplicate
      > PW module directory: https://modules.processwire.com/modules/inputfield-repeater-matrix-duplicate/
    • By gerritvanaaken
      Wouldn’t it be cool to have whole repeater items (or even repeater matrix items) language-specific. Some of my clients do have pages that are 90% identical in German and English, but there are some sections that should not appear in one or another language. I attached a concept screenshot.
      Would something like this be possible with some simple backend hooks and "hidden checkbox fields"? Or is it way more complicated?
       

    • By brdje
      Greetings,
       
      I've ran into an issue with adding a repeater field to a template.
      The page editor in the backend shows the fields and the data attached to the fields, including the repeater.
       
       
      However, when viewing the template in the front-end, there is no sign of a repeater field in the data set.
      ["data"]=> array(3) { ["name1189"]=> string(11) "partenaires" ["status1189"]=> string(1) "1" ["title"]=> string(8) "Partners" } } Do u have any idea what could be causing this issue?
      I've tried recreating the field and there is no caching active, but still can't get the repeater in the data set.
    • By ttttim
      Is there a way to add a (new or existing) page to the top of the asmSelect list? By default they're added at the bottom. 
×
×
  • Create New...