Forum Replies Created
-
AuthorPosts
-
June 26, 2023 at 4:53 pm in reply to: Results suggestions – how to add more than post tile in the results #44451
eli-leads-mania
ParticipantErnest thank you. Im also ok with paying for the custom tier support if that is what you are suggesting. Really your expert guidance is all I was looking because of your understanding on the plugin. Not the code so much. Please send me the details and pricing for custom support, im intereste.
June 26, 2023 at 1:32 pm in reply to: Results suggestions – how to add more than post tile in the results #44448eli-leads-mania
ParticipantIs it possible to create a similar search like yelp where inside the search there are 2 search fields – 1 with autosuggest that the user types in a keyword and on the other 2 – there is also a search field that displays the US cities. Similar to the previous function, the search fields will pull from 2 different json files. The keyword+city would be passed as parameters in in the url. This gives us a way to display results based on location.
June 25, 2023 at 10:52 pm in reply to: Results suggestions – how to add more than post tile in the results #44443eli-leads-mania
ParticipantHello Ernest,
I’m currently trying to modify the search results of the Ajax Search Pro plugin to display a custom sorted list of suggested keywords from a JSON file in my theme directory. Specifically, I want the results to be sorted by relevance to the search query, similar to Google’s search suggestions.
I’ve attempted to use the ‘asp_results’ filter to achieve this, with a function that reads the JSON file, creates a new stdClass object for each keyword, and appends it to the $results array. I’m using the levenshtein() and strpos() functions to calculate a relevance score for each keyword and sort the results accordingly.
However, I’ve encountered a couple of issues:
1. The keywords from my JSON file are being displayed, but the sorting does not seem to reflect the search query. They appear to be in a preset order that does not change.
2. When I modify the function to filter the results based on the search query before returning them, no results are displayed at all.I suspect there might be something internal to the plugin that is affecting the results. Would you be able to provide some guidance on how to correctly implement this functionality?
Here is the code I am currently using:
add_filter( ‘asp_results’, ‘asp_custom_keyword_results’, 10, 4 );
function asp_custom_keyword_results( $results, $search_id, $is_ajax, $args ) {
if ($search_id != 1) {
return $results;
}$search_term = $args[‘_ajax_search’];
$json_file = get_theme_file_path( ‘/data/keywords.json’ );
if (file_exists($json_file)) {
$json_data = file_get_contents($json_file);
$keywords = json_decode($json_data, true);$scores = [];
foreach($keywords as $keyword) {
$levenshtein_score = levenshtein(strtolower($search_term), strtolower($keyword));
$strpos_score = strpos(strtolower($keyword), strtolower($search_term));$result = new stdClass();
$result->id = null;
$result->title = $keyword;
$result->content = null;
$result->image = null;
$result->post_type = null;
$result->content_type = ‘keyword’;
$result->link = “/search?keyword=” . urlencode($keyword);$scores[] = [$strpos_score !== false ? $strpos_score : INF, $levenshtein_score];
$results[] = $result;
}array_multisort($scores, $results);
}return $results;
}I appreciate your help.
Everything else works. The keywords are passing the correct parameters that we can use to perform the search. The only thing thats left is to make the results seem more intuitive.
June 19, 2023 at 1:51 pm in reply to: Results suggestions – how to add more than post tile in the results #44364eli-leads-mania
ParticipantOk, i understand. you’re hooking into the asp_results filter and passing the parameters is_ajax search_id and rsults defining meta key and post type and using it or using the default get post meta.this is great, thank you! very helpful. I added:
$r->title .= “, ” . $state_code;
so i have the comma separator after the title. Im trying to dig into the documentation to really learn how to do all of the customizations i need. if you have any helpful links, please point me in the right direction.
For example, can the plugin index values from an acf relationship field where the values are returned as an array or do i need to write something to convert the array to a string
June 18, 2023 at 12:25 pm in reply to: Results suggestions – how to add more than post tile in the results #44347eli-leads-mania
ParticipantHi it looks like that i found a way display the results suggestions like i want using this:
Which is great. Is there a way that i can make titles specific to custom post type? For example:
cities post type: title, state_code
states post type: title
or any other titles description per post type
-
AuthorPosts