Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Results suggestions – how to add more than post tile in the results
- This topic has 9 replies, 2 voices, and was last updated 2 years, 11 months ago by
Ernest Marcinko.
-
AuthorPosts
-
June 18, 2023 at 10:58 am #44346
eli-leads-mania
ParticipantHi
So far I am happy with my purchase, however, i am going to need to add customizations to the plugin to help me achieve the results I need. I read through the documentation. I will write the functions necessary for the customization, please point me in the right direction.
For example, the results suggestions. I need to display custom suggestion names not just post title. Because I am trying to display the names of cities from a custom post type: ‘cities’ and in the US there are many cities with the same name. All cities posts have a custom field for state_code so I would like to display or post tile, state_code in the suggested results or pure custom fields – name, state_code.
In the front end filters of the documentation, Im only seeing things related to the search box. Can you send me a link or point me in the right direction how to hook into Results Suggestions?
June 18, 2023 at 12:25 pm #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
June 19, 2023 at 1:00 pm #44363Ernest Marcinko
KeymasterHi,
Thank you for your kind words!
Well, if the state_code exists on both post types, then it will display unfortunately for both, there is no conditional logic to that in the options.
However, this might be better to solve programmatically instead of using the advanced title/content fields, as if you change your mind or want to make it more complicated it will get worse.
Try adding this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.
add_filter( 'asp_results', 'asp_custom_link_meta_results', 10, 4 ); function asp_custom_link_meta_results( $results, $search_id, $is_ajax, $args ) { // Change this variable to whatever meta key you are using $meta_key = 'state_code'; $post_type = 'cities'; // --- no changes below foreach ($results as $k=>&$r) { if ( isset($r->post_type) && $r->post_type == $post_type ) { if ( function_exists('get_field') ) $state_code = get_field( $meta_key, $r->id, true ); // ACF support else $state_code = get_post_meta( $r->id, $meta_key, true ); if ( !empty($state_code) ) { $r->title .= " " . $state_code; } } } return $results; }I this code I assumed the post type name is
citiesand the meta field name isstate_code. You may have to adjust that on lines 5-6 to make sure it is correct.If you need help with the code let me know, I will add it for you.
June 19, 2023 at 1:51 pm #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 19, 2023 at 2:06 pm #44365Ernest Marcinko
KeymasterSure, no problem!
Generally I recommend looking at the hooks section of the knowledge base, most hooks are documented there.
Relationship fields should be indexed automatically when using the index table engine. It is not a 100% support unfortunately, so you will have to test, as every relationship field is a bit different. Generally speaking the plugin will try to fetch the values.
If you want to do some manual stuff to add to the index, then I would recommend using this hook. This one triggers whenever a post content is being indexed, right before it’s passed to the tokenizer. You can use this hook to change/add text to the content field. Generally it is a good execution point to add contents from custom fields or whatever field you wish, then the plugin will take care of the rest.
June 25, 2023 at 10:52 pm #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 26, 2023 at 1:32 pm #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 26, 2023 at 3:50 pm #44449Ernest Marcinko
KeymasterHi,
Before I start I need to inform you that I can’t promise anything as this falls way above the customization category and that is not officially part of our support. That being said I will of course try my best to get around this.
The code seems great to me, after the asp_results hook the plugin only executes a final results grouping function, but only if the results grouping feature is enabled, but in your case it is not. The order is not changed by the plugin afterwards.
In your code you do an array_multisort, however it does not seem correct to me, and also the $results array also contains the original results, it is not reset.
In your case the array_multisort will sort the$resultsarray (it will basically do nothing I believe), and then will try to sort the$scoresarray too with the same ordering as the$resultsarray. So it’s not doing what you think it’s doing.Instead of that, add the score to one of the results attributes like so:
$result->score = $strpos_score !== false ? $strpos_score : INF, $levenshtein_score;Then do a usort:
usort($results, function($a, $b){ if ( isset($a->score, $b->score) ) { if ( $a->score == $b->score ) { return 0; } return $a->score > $b->score ? -1 : 1; } else { if ( isset($a->score) ) { return 1; } if ( isset($b->score) ) { return -1; } } return 0; });This one is pretty ugly, but it should be close to a possible solution.
As for your other question, unfortunately it is not possible to create multiple inputs for the same form. You can however create an input custom field filter. Then you can fetch the value of that filter from the $args[‘post_meta_filter’] argument.
June 26, 2023 at 4:53 pm #44451eli-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 27, 2023 at 3:39 pm #44461Ernest Marcinko
KeymasterOh, I’m sorry, I was not implying that. I meant, that while I am more than happy to help, I can’t guarantee anything when it comes to customizations.
Unfortunately we can’t do custom support, I would love to offer, but there is barely enough time to do all the tasks so I wouldn’t be able to offer a quality service.
-
AuthorPosts
- You must be logged in to reply to this topic.