Jump to content

Having trouble returning specific child pages


floridaDev
 Share

Recommended Posts

I'm having some difficulties returning the sibling pages that have the same field value.

<?php
#SET VARS
$locationData = $page->get("LocationData"); 

# Get Sibling pages, except current one
$sibPages = $page->siblings("id!=$page");
# For each them
foreach ($sibPages as $sP) {
# Return Sibling pages that share the same "LocationData" field value
	$sP = $pages->find("LocationData=$locationData");
 # Another Loop
foreach($sP as $sPP) {
	echo $sPP->title .'<br />'; 
	}
}
?>

Page structure 

  • Parent Page 
  • - Child Page1 (locationData=foo)
  • - Child Page2(locationData=bar)
  • - Child Page3(locationData=foo
  • - Child Page4(locationData=foo

My desired output would be if I'm on child page 1, to return child page3, and child page4 because they have the same identical field value and are also a sibling page. Instead the output returns me Child page 1, child page 1, child page 3 child page 3, child page 4, child page 4 while being on child page 1. I understand this is happening because of my 2 foreach loop's but I'm not sure how to filter by $page then filter again by $pages.

TL;DR My end goal is to return sibling pages with the same field value, except the current page

Link to comment
Share on other sites

40 minutes ago, Martijn Geerts said:

Something like this ?:


$id = $page->id;
$pid = $page->parent->id;
$locationData = $page->get("LocationData");
$relatedPages = $pages->find("parent=$pid, id!=$id, LocationData=$locationData");

 

That did it, thanks a lot. I didn't realize you can include parameters like that in the $pages selector. 

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

  • Recently Browsing   0 members

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