试图从Woocommerce搜索查询中删除复数“s”,但这只是删除结尾的每个字母——我做错了什么?谢谢
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\');
最合适的回答,由SO网友:Pim 整理而成
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\');