Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Ordering Search Results
This topic contains 11 replies, has 2 voices, and was last updated by Ernest Marcinko 4 years, 2 months ago.
- AuthorPosts
- February 14, 2019 at 11:48 pm #21130
Hello,
I have two questions about your plugin.
1. Is it possible to arrange the Woocommerce categories before the Woocommerce products in the search results?
2. Is it possible that the top categories appear first in the search results and then the subcategories?
For example: In the search term “Computer”, the top category “Computer, Tablet & Zubehör” will be displayed or ordered after a few
subcategories of “Computer, Tablet & Zubehör”. (Image)I hope you can help me.
Kind Regards
Lucas
Attachments:
You must be logged in to view attached files.February 18, 2019 at 10:02 am #21146Hi!
1. Yes, with the mixed results ordering, you can change which content type should be displayed first: https://i.imgur.com/VVmUnuJ.png
2. I’m afraid this is not possible, I’m sorry.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
February 20, 2019 at 6:05 pm #21209Thank you for your message.
2. Is there really no way that only the top categories and
then the respective subcategories are displayed?Thank you.
February 21, 2019 at 2:20 pm #21214Hi,
2. Well, the term order is not stored within the database, so there is only a ‘hacky’ way of doing it by using a custom code. The internal wordpress method does not work here, as that only works for the complete list of categories, but not partial results.
I have constructed a custom code, that may work in most cases, ordering the child categories below their parents. It’s definitely not the best solution, but it may do the trick.Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
Best,add_filter('asp_results', 'asp_sort_terms_by_parent', 10, 1); function asp_sort_terms_by_parent( $results ) { $term_ids = array(); $res_terms = array(); $sorted_terms = array(); $final_terms = array(); foreach ( $results as $k => $r ) { if ( $r->content_type == 'term' ) { $term_ids[] = $r->id; $res_terms[$r->id] = $r; unset($results[$k]); } } if ( count($term_ids) > 0 ) { $terms = get_terms(array( 'include' => $term_ids )); wd_sort_terms_hierarchicaly($terms, $sorted_terms); wd_flatten_hierarchical_terms( $sorted_terms, $terms ); foreach ($terms as $k => $t) { $final_terms[] = $res_terms[$t->term_id]; } } $results = array_merge($final_terms, $results); return $results; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 10, 2019 at 12:13 am #21539Hello,
Unfortunately, the code does not work for me. The subcategories are still displayed before the overcategories. Do I have to change anything in the Ajax Search Pro settings for the code to work?
Thanks for your help.
March 11, 2019 at 11:08 am #21554Hi,
Unfortunately this does not depend on the back-end options, the code is executed after the search process is finished.
If you want, you can add temporary FTP access and I can take a look at the code, maybe I can adjust it. Although I don’t know if there is a solution to be honest. The term ordering solution within the WordPress core is also pretty problematic code, and it does not work when the term search argument is passed either – so unfortunately I can’t re-use that for a solution.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 11, 2019 at 5:50 pm #21566You cannot access this content.March 12, 2019 at 9:53 am #21576Thank you very much. I may have found a possible solution, that may work in most cases, but to be honest I am not entirely sure. I conducted a few tests, and it seemed to work normally:
– Test 1: https://i.imgur.com/bteS8mb.png
– Test 2: https://i.imgur.com/ogioCKH.pngThe final code is below. You can change the $sub_string variable on line 3, that is printed before each sub category.
Best,add_filter('asp_results', 'asp_sort_terms_by_parent', 10, 1); function asp_sort_terms_by_parent( $results ) { $sub_string = '—'; // The string printed before each sub-level category // --- DO NOT CHANGE ANYTHING BELOW --- $term_ids = array(); $res_terms = array(); $sorted_terms = array(); $final_terms = array(); foreach ( $results as $k => $r ) { if ( $r->content_type == 'term' ) { $term_ids[] = $r->id; $res_terms[$r->id] = $r; unset($results[$k]); } } if ( count($term_ids) > 0 ) { $terms = get_terms(array( 'include' => $term_ids, 'hide_empty' => false )); wd_sort_terms_hierarchicaly($terms, $sorted_terms); wd_flatten_hierarchical_terms( $sorted_terms, $terms ); foreach ($terms as $k => $t) { $tt = $res_terms[$t->term_id]; $tt->level = $t->level; $final_terms[] = $tt; } foreach ($final_terms as &$fr) { if ( $fr->level > 0 ) $fr->title = str_repeat($sub_string, $fr->level) . ' ' . $fr->title; } } $results = array_merge($final_terms, $results); return $results; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 17, 2019 at 2:00 am #21642Hello,
Many thanks for your help. The code works fine. I have not noticed any problems so far.
But I have two more questions.
1.) If I enter the search term “HERREN”, the category “HeckenscHERREN & GrasscHERREN” is displayed. Which setting should I choose in Search Logic to prevent this? (Image Search Logic )
2.) In WordPress, I get a message from Ajax Search Pro, I should clear the cache. Can I safely delete the cache in Cache Settings? (Image Cache)
Many thanks for your help.
Kind Regards
Attachments:
You must be logged in to view attached files.March 18, 2019 at 10:46 am #21656Hi,
1. In this case, I would probably recommend the ‘and with exact keyword matches’ logic: https://i.imgur.com/rlMCHl5.png
2. You can safely ignore that message, it refers to the site cache, and CDN, but it your case it is not an issue.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 27, 2019 at 12:24 am #21811Hello,
I switched to “And with exact keyword matches”.
But unfortunately it no longer shows “Herrenmode” when searching for “herren”.
Do I have to change anything else at Secondary Logic?Thanks for your help.
Best,
LucasMarch 27, 2019 at 11:52 am #21834Hi,
Well, maybe trying one of these two combinations:
– Combination 1: https://i.imgur.com/yfQMscx.png
– ..or comtination 2: https://i.imgur.com/GMcyzKv.pngOne of those might be the closest to your needs.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.