Skip to content

Instantly share code, notes, and snippets.

@texnixe
Last active June 15, 2017 12:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save texnixe/032c6e875f959989227b90a9f4f45780 to your computer and use it in GitHub Desktop.
Save texnixe/032c6e875f959989227b90a9f4f45780 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