I'm on Modx Revolution 2.6.0-pl, PHP 5.5.38, MySQL 5.6.23.
I have a snippet that gets a list of all resources on the site (with some filtering) for use in a single-select TV. Everything works fine until I also try to get the parent page titles of all of those resources to help make selecting the correct page a little easier as some of my pages have identical pagetitles.
Below is the snippet that works:
<?php
$excludeParents = array(42,2,41,31,25,22);
$c = $modx->newQuery('modResource');
$c->where(array(
'modResource.parent:NOT IN' => $excludeParents,
'modResource.id:NOT IN' => $excludeParents
));
$resources = $modx->getCollection('modResource',$c);
$pageList = [];
foreach ($resources as $resource) {
$pageList[] = $resource->get('pagetitle') . '==' . $resource->get('id');
}
$output = implode("||",$pageList);
return $output;
Below is one of my attempts at getting the parent page titles, this results in a 500 error with no errors being logged in Modx or in server error_logs that I can find.
<?php
$excludeParents = array(42,2,41,31,25,22);
$c = $modx->newQuery('modResource');
$c->where(array(
'modResource.parent:NOT IN' => $excludeParents,
'modResource.id:NOT IN' => $excludeParents
));
$resources = $modx->getCollection('modResource',$c);
$pageList = [];
foreach ($resources as $resource) {
$parentID = $resource->get('parent');
$parent = $modx->getObject('modResource', $parentID);
$pageList[] = $resource->get('pagetitle') . ' ('. $parent->get('pagetitle') .')==' . $resource->get('id');
}
$output = implode("||",$pageList);
return $output;
If I just use $parentID in place of $parent->get('pagetitle'), it shows the parent ID's.
I thought maybe Modx didn't like trying to get page titles for ID's of 0, so I also tried adding an if statement to only try to get parent titles if the ID wasn't 0. However, this still results in a 500 error.
<?php
$excludeParents = array(42,2,41,31,25,22);
$c = $modx->newQuery('modResource');
$c->where(array(
'modResource.parent:NOT IN' => $excludeParents,
'modResource.id:NOT IN' => $excludeParents
));
$resources = $modx->getCollection('modResource',$c);
$pageList = [];
foreach ($resources as $resource) {
$parentID = $resource->get('parent');
$parentTitle = '';
if($parentID !== '0'){
$parent = $modx->getObject('modResource', $parentID);
$parentTitle = $parent->get('pagetitle');
}
$pageList[] = $resource->get('pagetitle') . ' ('. $parentTitle .')==' . $resource->get('id');
}
$output = implode("||",$pageList);
return $output;
I appreciate any help!