0

Trying to remove plural 's' from Woocommerce search Query but it's just removing every letter of the end - what am I doing wrong? Thanks

function mySearchFilterFunction($query) {

if ( !is_admin() && $query->is_main_query() && $query->is_search) {

$search_term = $query->get('s');
$query->set('s', substr($search_term, 0, -1));

    }
}

add_action( 'pre_get_posts', 'mySearchFilterFunction');
2

Try this:

function mySearchFilterFunction($query) {
  if ( !is_admin() && $query->is_main_query() && $query->is_search) {
    $search_term = $query->get('s');
    if (substr($search_term, -1) == 's'){
      $search_term = substr($search_term, 0, -1);
    }
    $query->set('s', $search_term);
  }
}
add_action( 'pre_get_posts', 'mySearchFilterFunction');
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.