This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Relevance sorting on variables

#27482
Ernest MarcinkoErnest Marcinko
Keymaster

Hi,

I checked the configuration, and selected all missing atrributes on the index table panel I could find. Still, the problem is, that the storage string is stored as “64 GB” and not “64GB”. So searching “64GB” will never match the string “64 GB” due to the whitespace chararcter.

I think that the best option would be to detect these search strings, and ditch the “GB” part, and only keep the number. For that the only way is to use a custom code. Based on this tutorial, I would recommend this modified version of the code:

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_query_args', 'asp_replace_search_keywords', 10, 2 );
function asp_replace_search_keywords( $args, $id ) {
  /**
   * Add as many words as you want. On the left side place the
   * word or words separated by comma to replace.
   * To the right side, place the single word to replace it with.
   */
  $replace = array(
    '8GB,16GB,32GB,64GB,128GB,256GB,512GB,1024GB' => '8,16,32,64,128,256,512,1024'
  );

  // ----------------------------------------
  // Do not change anything below this line
  // ----------------------------------------
  foreach ( $replace as $k => $w ) {
    $kr = explode(',', $k);
	$wr = explode(',', $w);
    foreach ($kr as $krk => &$krv) {
      $krv = trim($krv);
      if ( $krv == '' ) {
        unset($kr[$krk]);
      } else {
		$krv = '/\b' . $krv . '\b/u';
	  }
    }
    foreach ($wr as $wrk => &$wrv) {
      $wrv = trim($wrv);
      if ( $wrv == '' ) {
        unset($wr[$wrk]);
      }
    }
    if ( count($kr) > 0 ) {
      $args['s'] = preg_replace($kr, $wr, $args['s']);
    }
  }
  return $args;
}

Please be very careful, I have not been able to test this code!