Not quite. As I said above, there is no search function for files. And if you use a variable, you may not put it between quotes, otherwise it’s interpreted as a string.
<?php
return function($site, $pages, $page) {
$query = get('q');
$results = $pages->files()->filter(function($file) {
return $file->caption() == $query;
});
return array(
'query' => $query,
'results' => $results,
);
}
But for a simple filter like that, you don’t need the callback.
But if you use the callback like above, you can implement more complicated logic, like loop through all fields to check if they contain the search value etc. or look for the search string in more than one field, e.g.
<?php
return function($site, $pages, $page) {
$query = get('q');
$results = $pages->files()->filter(function($file) {
return $file->caption() == $query || in_array($query, $file->tags()->split(',');
});
return array(
'query' => $query,
'results' => $results,
);
}
Or maybe you want to check if the search term is a substring of the caption:
<?php
return function($site, $pages, $page) {
$query = get('q');
$results = $pages->files()->filter(function($file) {
return str_pos($file->caption(), $query) || in_array($query, $file->tags()->split(',');
});
return array(
'query' => $query,
'results' => $results,
);
}