考虑到MySQL中缺少preg_replace
- 以下使用嵌套REPLACE
要从标题中删除所有数字,请执行以下操作:
function wpse167989_posts_orderby( $orderby, $query ) {
global $wpdb;
// Strip all numbers from title, and trim any leading spaces.
return $wpdb->prepare(
\'LTRIM(\' . str_repeat( \'REPLACE(\', 10 ) . $wpdb->posts . \'.post_title\'
. str_repeat( \', %s, %s)\', 10 ) . \') \' . $query->get( \'order\' )
, 0, \'\', 1, \'\', 2, \'\', 3, \'\', 4, \'\', 5, \'\', 6, \'\', 7, \'\', 8, \'\', 9, \'\' );
}
那么
add_filter( \'posts_orderby\', \'wpse167989_posts_orderby\', 10, 2 );
$loop = new WP_Query( $args );
remove_filter( \'posts_orderby\', \'wpse167989_posts_orderby\', 10 );
按注释编辑:若要删除连字符,过滤器将变为
function wpse167989_posts_orderby( $orderby, $query ) {
global $wpdb;
// Strip all numbers & hyphens from title, and trim any leading spaces.
return $wpdb->prepare(
\'LTRIM(\' . str_repeat( \'REPLACE(\', 11 ) . $wpdb->posts . \'.post_title\'
. str_repeat( \', %s, %s)\', 11 ) . \') \' . $query->get( \'order\' )
, 0, \'\', 1, \'\', 2, \'\', 3, \'\', 4, \'\', 5, \'\', 6, \'\', 7, \'\', 8, \'\', 9, \'\', \'-\', \'\' );
}