Jump to content

Looping through finding ProcessWire Pages with an array


Recommended Posts

On the site I run, we present a page for each day of the year. Each page is found using page reference fields because the date that the page presented moves every year, and depending on the day or time of year the page can have a different template. (More background on this previous post if helpful) I currently have a 'relevant pages' section at the top of a page which presents the page for today's date, the previous 2 days, and the next 2 days.

The way I've approached this so far is to run a separate MySQL query for each day, load it into a variable, and then run another query, load that into a variable, etc.  However, the problem with this is that I'm having to repeat the code for each variable. So for example, create a selector "$find_less_two" for 2 days before today:

$find_less_two = "
    WHERE `date` = SUBDATE(CURDATE(),2)
    if ($result = $conn->query($find_less_two)) {
        while ($row = $result->fetch_assoc()) {
            $sundayyearvar = $row["sunday_year"];
            $weekdayyearvar = $row["weekday_year"];
            $seasonvar = $row["season"];
            $weekvar = $row["week"];
            $dayvar = $row["day"];
            $solemnityvar = $row["solemnity"];
            $typevar = $row["type"];
            $feastvar = $row["feast1"];
        if ($typevar == 'season') 
            $date_less_two = $pages->find([
                'template' => 'season-homily',
                'Liturgical_Season.title' => $seasonvar,
                'Liturgical_Week.title' => $weekvar,
                'Day' => $dayvar
        elseif ($typevar == 'solemnity') 
            $date_less_two = $pages->find([
                'template' => 'solemnity',
                'Solemnity' => $solemnityvar,
                'Liturgical_Season.title' => $seasonvar
        etc etc for each '$typevar' that needs to be considered.

Rinse and repeat, each time, 5 times. Then, in the PW template, I load the selector that's been populated from the database. That means that for each "$find_day_x" selector, it runs the "find" function and populates a styled front-end:

foreach ($date_less_two as $datetwo) {
	echo "
		<a href='$datetwo->url' class='day' style='border-top-color:$colour;'>
		<div class='relevant-date'>{$datetime->date($format = 'l', $ts = '-2 day')}</div>";
    	if($datetwo->Solemnity) {
    		echo "<div class='relevant-solemnity'>{$datetwo->Solemnity->title}</div>";
    		echo "<div class='relevant-title'>{$datetwo->title}</div></a>";
foreach ($date_less_one as $dateone) {
 (etc etc)

Again, this means that I have to repeat the code for each selector separately, just to get it to appear as 5 separate days.

I honestly can't believe this is the most efficient way, so instead I struck upon the idea of loading all 5 day's results straight into an array from the database. However, I can't find a way to get PW to iteratively $find each page based on that array. Works well enough with one array, but that's basically what I've been doing up until now anyway. I was trying to get away from loading each page individually, but I've run out of ideas.

Does anyone have any ideas for how I could make this more efficient? Really, I'd like to extend it well beyond 5 days to cover a whole month or year in a calendar format, but I don't fancy the overhead of running each day as a separate selector for 365 days...

Link to comment
Share on other sites

If I'm understanding what you're trying to achieve correctly (which I may not be!), might something like this work?

$twoDaysAgo = strtotime('-2 days');
$twoDaysAhead = strtotime('+2 days');
$pagesInDateRange = $pages->find("template=templatenameA, datefield>=$twoDaysAgo, datefield<=$twoDaysAhead, sort=datefield");
foreach($pagesInDateRange as $pageIDR) {
    if($pageIDR->type == 'season') {
         // Using just two generalised field and variable names as an example
         $var1 = $pageIDR->field1;
         $var2 = $pageIDR->field2;
         $relevantPages = $pagesInDateRange->find("template=templatenameB, fieldA=$var1, fieldB=var2, sort=datefield");
  	} elseif ($pageIDR->type == 'solemnity') {
        // etc etc
    foreach($relevantPages as $rPage) {
        // Render pages

This could all go in the template for the page.

  • 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

  • Recently Browsing   0 members

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