Skip to content

Instantly share code, notes, and snippets.

@pedroborges
Last active December 1, 2017 23:03
Show Gist options
  • Save pedroborges/3a9ddb1a0a94a5e73d16fcd616f3dbc5 to your computer and use it in GitHub Desktop.
Save pedroborges/3a9ddb1a0a94a5e73d16fcd616f3dbc5 to your computer and use it in GitHub Desktop.
Besides finding non-exact matches in your content, Fuzzy Search for @getkirby offers some neat features like the ability to search through a custom page method. https://github.com/pedroborges/kirby-fuzzy-search
<?php
// site/controllers/blog.php
return function($site, $pages, $page) {
$query = esc(get('q'));
$articles = $page->children()->visible()->flip();
if ($query) {
$articles = $articles->fuzzySearch($query, 'title|text|authorName');
}
$perpage = $page->perpage()->int();
$articles = $articles->paginate($perpage >= 1 ? $perpage : 5);
$pagination = $articles->pagination();
return compact('articles', 'pagination', 'query');
};
<?php
// site/plugins/methods.php
page::$methods['authorName'] = function($page) {
$author = $page->author()->value();
if ($user = site()->user($author)) {
return $user->firstname().' '.$user->lastname();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment