• Resolved stoelwinder

    (@stoelwinder)


    My products have the Yoast SEO parameters for UPC and MPN configured and when I search the website, it shows me the product as the first entry, however when I search from the admin interface, I get no results. This happens for any product I try and search by MPN.

    Also, although I dont think this is related to Relevanssi, when I try to search on the website, I have an AJAX popup return the results visually first. Relevanssi is not integrated with this search either (so when I search something, it first says no results but once I press Enter, the results show up).

    Any suggestions on what I might be doing wrong?

Viewing 4 replies - 31 through 34 (of 34 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Just debug it more, I suppose? This should ensure the products are in the right order, so either 1) they’re not put in the right bin to begin with, or 2) something changes the order afterwards.

    If you check what is inside $exact_match before the array_merge(), do you have anything in it?

    Thread Starter stoelwinder

    (@stoelwinder)

    Yeah I managed to figure out what is happening. $hits[1] contains the search result after it’s been “filtered” by Relevanssi. So it will remove dashes, etc according to the settings. It will also turn the search term into lowercase, which means its not a match to $_post->post_title anymore.

    So, I either need to “modify” each $_post->post_title through the same process to the same format. Is there a function that converts the search terms to the specified format?

    Situation now:
    Search Term: Day of the Tentacle (PC)
    $hits[1] contains: day of the tentacle (pc)
    $_post->post_title contains: Day of the Tentacle (PC)

    Converting all to lowercase is easy enough, but what if it is Day-Of.The-Tentacle (pc). $hits[1] will then still contain day of the tentacle (pc) but $_post->post_title would contain day-of.the-tentacle (pc) and still not match.

    Is there a way to convert each $_post->post_title to the same format? Or get access to the original search term? Or to ignore all dashes, dots and uppercase letters?

    2. Another unintended side-effect: Admin search now also puts out of stock all the way at the back. Can I set it so it only applies the relevanssi_hits_filter on public searches?

    Plugin Author Mikko Saari

    (@msaari)

    1. You can get the raw, unfiltered search query from get_search_query().

    2. You can check for is_admin() and if that’s true, return $hits untouched.

    Thread Starter stoelwinder

    (@stoelwinder)

    Thank you (again). I’ve done some modifications. Now, the results will only show for public (non-admin) searches using the is_admin() function. It will also do a case-insensitive substring check to see if the search term is in the title (I’m not accounting for words spread through the product title, as it would be quite labor intensive) and I also check whether there’s in stock matches first.

    This way, you will get:
    First part of the results: in stock and matching search term
    Second part of the results: out of stock and matching search term
    Third part of the results: everthing else
    Last part of the results: out of stock

    Sharing the code here in case someone else can benefit from it:

    add_filter( 'relevanssi_hits_filter', 'rlv_bucket_sorting' );
    function rlv_bucket_sorting ( $hits )  {
      if (is_admin()) return $hits;
      $original_search = get_search_query();
      $out_of_stock	= array();
      $everything_else = array();
      $exact_match_stock = array();
      $exact_match_nostock = array();
      foreach ( $hits[0] as $_post ) {
        $post_object = relevanssi_get_an_object( $_post )['object'];
        $product = wc_get_product( $post_object );
        if ((! $product->is_in_stock()) && (stristr($_post->post_title, $original_search) == TRUE)) {
          array_push($exact_match_nostock, $_post);
        } elseif (( $product->is_in_stock()) && (stristr($_post->post_title, $original_search) == TRUE)) {
          array_push($exact_match_stock, $_post);
        } elseif ($product->is_in_stock()) {
    	array_push($everything_else, $_post);
        } else {
          array_push($out_of_stock, $_post);
        }
      }
      $hits[0] = array_merge( $exact_match_stock, $exact_match_nostock, $everything_else, $out_of_stock );
      return $hits;
    }

    Thanks again for sticking with me and my continuous questions!

Viewing 4 replies - 31 through 34 (of 34 total)
  • The topic ‘Website Shows Results but Admin doesnt’ is closed to new replies.