Can I search() the nested fields of a users field? Let’s say I want to search the location field of all the users that are saved to a users field on a given page.
This is of course nonsense but would be what I need. Any ideas?
$results = $conversations->search($query, 'users->location');
No, that’s not possible. The search method only searches inside the fields in the page.
I think you need multiple steps to achieve this:
- get the users from the pages
- search within those users’ location field
- then somehow map that to the pages again
Have to think about this some more…
Okay, that’s what I thought. Or I could use a page.update:after hook to save the users’ locations to a hidden field on the page, which could then be searched . Not very elegant, but could do the trick.
Thanks for the quick feedback!
I got it working like this now:
/* Search */
$query = get('q');
if ($query) {
$results = [];
/* Search Conversation Title */
$resultsConversations = $conversations->search($query, 'title');
foreach ($resultsConversations as $result) {
$results[] = $result;
}
/* Search Users */
$resultsUsers = $kirby->users()->search($query, 'location|country');
foreach ($resultsUsers as $result) {
$results = A::merge($results, $result->getConversations());
}
$conversations = pages($results)->sortBy('lastPhotographModified', 'desc');
}