global $wpdb;
$sql = "((CASE WHEN {$wpdb->posts}.post_title LIKE '%s' THEN 2 ELSE 0 END) + (CASE WHEN {$wpdb->posts}.post_content LIKE '%s' THEN 1 ELSE 0 END)) DESC, {$wpdb->posts}.post_modified DESC, {$wpdb->posts}.ID ASC";
$search = esc_sql(urldecode(stripslashes($_REQUEST['s'])));
return sprintf($sql,$search,$search);
这是相关性排序吧?
跟lz说下,wp还对$_REQUEST['s']进行了一些处理.
比如test1 test2
会分成
$wpdb->posts.post_title LIKE '%test1%' and $wpdb->posts.post_title LIKE '%test2%'
其实还会search content,略过