Last active
June 15, 2017 12:25
-
-
Save texnixe/032c6e875f959989227b90a9f4f45780 to your computer and use it in GitHub Desktop.
Kirby custom pages method to filter by values in structure field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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