Hello,
I added author in search query and it works, but it only search authors username. is their a way to include first name and last name?
heres my controller
return function($site, $pages, $page) {
$Numberoftags = page('blog')->number_of_popular_tags()->int();
//$whatToSearch = $page->whattosearch()->text();
//$NumberofArticletobePaginated = $page->number_article_paginated()->int();
// if ($Numberoftags <= 0 || $NumberofArticletobePaginated <= 0) {
// $Numberoftags = 5;
// $NumberofArticletobePaginated = 3;
// }
$tagcloud = tagcloud(page('blog'), array('limit' => $Numberoftags));
$query = get('search-field');
$results = page('blog')->search($query, 'title|text|author|tags');
//$results = $site->search($query, 'title|text');
$results = $results->paginate(9);
$searchCount = count(page('blog')->search($query, 'title|text|author|tags'));
return array(
'query' => $query,
'results' => $results,
'pagination' => $results->pagination(),
'tagcloud' => $tagcloud,
'searchCount' => $searchCount
);
};
The problem is that your author field only contains the username, not first and last name. If you want to search for first and last name, you would have to store those values in your content file instead of the username.
Another option that requires some coding, would be to create your own search method.
Can you not look up the user name and get the first and last names that way, rather then store them in the content? Im sure i did this recently.
texnixe
October 25, 2017, 11:18am
4
Well, if the user searches for first and last name, the username is quite useless.
So if you want the user to be able to search for last and first name, you have to store the name, not the username.
But i built a client portal recently that displays the users First Name and Last Name from a given username. Surely this can be added to the data used for searching. I had something like…
<?php if($user = $site->user()): ?>
<h1>Welcome <?= esc($user->firstName() . ' ' . $user->lastName()) ?></h1>
<?php endif ?>
Does this literally only accept physical fields on the page?
$results = page('blog')->search($query, 'title|text|author|tags');
What im getting at is cant you fool it into taking into account the first and last names. maybe im not explaining what i mean very well or missing a point somewhere.
texnixe
October 25, 2017, 11:27am
6
The search function searches the text files, not something you print on a page.
Of course you can get the first name and last name from the user, but that doesn’t help you any.
As i thought i hadnt explained very well…
What i mean is, can you not get it to turn the username from the field on the page into the full first name and last name by looking it up?
so the search knows that “John Smith” is actually “siteuser12”.
Do you get what i mean?
texnixe
October 25, 2017, 11:41am
8
If anything, you’d have to do it the other way round:
User enters search term and submits the form
Controller checks if that search term can be found in the user data and returns matching usernames
Search searches for those usernames
Search returns results for usernames
If it is more likely that the user searches by last and first names rather then username, the easiest way would be to store that data in the files.
Thats what i was getting at … only had one of cup of tea this morning.
Except your last comment… in my head, combining two arrays, one with all data except author, and the other with usenames translated into first and last names and searching THAT combined array would work.
After all isnt that what the search helper does anyway, its a shortcut to creating an array of stuff to search.
That way, you could search for “John Smith” and the search results would return pages created by “User12”.
texnixe
October 25, 2017, 12:05pm
10
Maybe I’m stupid, by I have no idea what two arrays you are talking about. Where do these arrays come from?
I mean build up the array that gets searched by hand rather than with ->search($query, 'title|text|author|tags')
.
so do ->search($query, 'title|text|tags')
instead combined with an array that translates the usernames into first and last names.
texnixe
October 25, 2017, 12:11pm
12
And then? How do you search the author?
Store it as a json file and search it either with php or javascript, similar to what i did here
texnixe
October 25, 2017, 12:16pm
14
Then you might as well not use the built-in search at all.
I know this far from ideal as I usually prefer going with Algolia for better search results and have it index the user formatted name.
Anyway, I decided to try a solution using Kirby’s built-in search, and it works:
$posts = page('blog')->map(function($post) {
$author = 'retrieve author formatted name';
$post->content()->data['author'] = new Field($post, 'author', $author);
return $post;
});
$results = $posts->search($query, 'title|text|author|tags');
Using the map
method we are able to loop over blog posts and change the author
field before calling search
.
2 Likes
texnixe
October 25, 2017, 12:24pm
16
@pedroborges : Perfect use of the map method, should have thought of that.
The thing with Algolia is, that it is only as good as the data you provide, so you would have to take care of that.
@pedroborges Nice one! I had a feeling it was possible.
texnixe
October 25, 2017, 12:27pm
18
Maybe even map a new field instead, so you could in fact search both, the original author field and the virtual full name field.
Yep, that’s a good idea too.