Skip to content

Instantly share code, notes, and snippets.

@mauricerenck
Forked from texnixe/filterbystructure.php
Created June 15, 2017 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricerenck/a1b986d91326290c63a879671718f4a4 to your computer and use it in GitHub Desktop.
Save mauricerenck/a1b986d91326290c63a879671718f4a4 to your computer and use it in GitHub Desktop.
Kirby custom pages method to filter by values in structure field
<?php
/* Usage example:
* $filteredPages = $page('projects')->children()->visible()->filterByStructure('structureField', array(
* 'field1' => $value,
* 'field2' => $value
* ));
*/
pages::$methods['filterByStructure'] = function($pages, $field, $options) {
$filteredPages = $pages->filter(function($p)use($field, $options) {
$structureField = $p->$field()->yaml();
foreach($structureField as $item) {
if(!array_diff($options, $item)) return $p;
}
});
return $filteredPages;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment