Ordering Search Results

This topic contains 11 replies, has 2 voices, and was last updated by

 
Keymaster
2 years, 10 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #21130

    Participant

    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.
    #21146

    Hi!

    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 :)


    #21209

    Participant

    Thank you for your message.

    2. Is there really no way that only the top categories and
    then the respective subcategories are displayed?

    Thank you.

    #21214

    Hi,

    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!

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #21539

    Participant

    Hello,

    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.

    #21554

    Hi,

    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 :)


    #21566

    Participant
    You cannot access this content.
    #21576

    Thank 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.png

    The final code is below. You can change the $sub_string variable on line 3, that is printed before each sub category.

    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #21642

    Participant

    Hello,

    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.
    #21656

    Hi,

    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 :)


    #21811

    Participant

    Hello,

    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,
    Lucas

    #21834

    Hi,

    Well, maybe trying one of these two combinations:

    – Combination 1: https://i.imgur.com/yfQMscx.png
    – ..or comtination 2: https://i.imgur.com/GMcyzKv.png

    One 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 :)


Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.