• Resolved alessandrafranco

    (@alessandrafranco)


    Hi! I need to set the order of the search results to show by menu order but I couldn’t find anywhere how to do that! Is it possible? I really apprecciate if anyone could help me with this! Thanks!

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Adding

    add_filter( 'relevanssi_orderby', function() {?return array( 'menu_order' => 'asc' ); } );

    to your theme functions.php will order the search results by menu_order ascending.

    Thread Starter alessandrafranco

    (@alessandrafranco)

    Hi Mikko!Thanks for the fast return! I tried to add this code on my theme functions.php but it isn’t working. ?? it’s still ordering the search results by relevance.

    Plugin Author Mikko Saari

    (@msaari)

    Can something else be overriding this? If you change the sorting order from Relevanssi settings to date sorting, does that have any effect on the results?

    Thread Starter alessandrafranco

    (@alessandrafranco)

    Hi Mikko! I tried to change the sorting order from Relevanssi settings to date sorting and it didn’t had any effect on the results

    Thread Starter alessandrafranco

    (@alessandrafranco)

    Mikko, I tried again to add the code in my function.php and notice that it changes the results but it doesn’t show the results that should show using menu order sort settings.

    Plugin Author Mikko Saari

    (@msaari)

    Could you perhaps give me an example of a search that produces a small number of results and where the the results are not in the correct order, and what the correct order should be?

    Thread Starter alessandrafranco

    (@alessandrafranco)

    Sure! Please, search for “Corretivos” on https://www.papelariacriativa.com.br if the sorting mode was correctly seted to show results by menu_order, this search must first show the products with images but this is not happening.

    Plugin Author Mikko Saari

    (@msaari)

    I searched for “corretivos”, and only got results that are products with images. The search didn’t find any products that didn’t have an image. What is the correct order for those products, and which products are in the wrong order exactly? I can’t see the menu_order, so I can’t tell myself.

    Thread Starter alessandrafranco

    (@alessandrafranco)

    Hi Mikko! Are you sure? Did you checked the entire page? From the 6a row forth it shows a few products without images among products with images (First one without image that appear among the products with image is “CORRETIVO FITA CIS TAPE MINI 4MTS”. And if the sort mode was obeying the menu_order classification, the results should show all the products with imagens first because they are all with an -1 on the menu_order field. The products without image should appear only on the end.
    If you click on the category ” Corretivo” (Right sidebar menu “Categorias” : Material Escolar > Corretivos) You will see what I mean because on the categories navigation the product sorting are correctly seted to show products by menu_order.

    Plugin Author Mikko Saari

    (@msaari)

    Ah, when I search for “corretivo”, I get the results you meant.

    Well, I don’t know. Sorting by “menu_order” is quite unusual, and there are other ways to handle imageless results that are better suited for Relevanssi (like sorting by relevance and then adding a filter that pushes the imageless products to the bottom of the list).

    But you may want to try adding this to your theme functions.php:

    add_filter( 'relevanssi_missing_sort_key', 'rlv_default_key', 10, 2 );
    function rlv_default_key( $value, $key ) {
        if ( 'menu_order' === $key ) {
            $value = PHP_INT_MAX;
        }
        return $value;
    }

    See if this does something.

    Thread Starter alessandrafranco

    (@alessandrafranco)

    Hi Mikko! Thanks for your help! I tried to add this code on my theme functions.php but I’m getting this error:

    The changes to your PHP code were undone due to an error on line 38 of the wp-content / themes / papelariacriativa / functions.php file. Please correct and try to save again.

    syntax error, unexpected ‘&’

    Plugin Author Mikko Saari

    (@msaari)

    Well, there’s no & in the code I sent, so… I don’t know how is that error related. What’s on the line 38 of your functions.php?

    Thread Starter alessandrafranco

    (@alessandrafranco)

    Sorry Mikko! I copyed the code from my e-mail and for some reason it was different there so it returned this error. I tried again copying the code from here and it didn’t returned any error but it didn’t changed the results as well.
    We can try the other way you said it was easier to show the imageless products on the bottom of the list? What should I do?

    Plugin Author Mikko Saari

    (@msaari)

    Are the images simply the default featured images in WordPress? If you’re using featured images, then something like this:

    add_filter( 'relevanssi_hits_filter', 'rlv_imageless_filter' );
    function rlv_imageless_filter(?$hits ) {
        $imageless  = array();
        $with_image = array();
        foreach ( $hits[0] as $hit ) {
            if ( has_post_thumbnail( $hit->ID ) ) {
                $with_image[] = $hit;
            } else {
                $imageless[] = $hit;
            }
        }
       ?$hits[0] = array_merge( $with_image, $imageless );
        return $hits;
    }

    Now the posts with images should always be on top.

    Thread Starter alessandrafranco

    (@alessandrafranco)

    Thank you so much Mikko! This last code worked perfectly! You are amazing helping me so kindly with this! I really needed to fix this and I wouldn’t get it without your help! Thanks once again!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘How to order search results by menu order’ is closed to new replies.