add_filter('posts_orderby_request', 'wpjam_search_orderby_filter'); function wpjam_search_orderby_filter($orderby = ''){ if(is_search()){ global $wpdb; $keyword = $wpdb->prepare($_REQUEST['s']); return "((CASE WHEN {$wpdb->posts}.post_title LIKE '%{$keyword}%' THEN 2 ELSE 0 END) + (CASE WHEN {$wpdb->posts}.post_content LIKE '%{$keyword}%' THEN 1 ELSE 0 END)) DESC, {$wpdb->posts}.post_modified DESC, {$wpdb->posts}.ID ASC";
}else{ return $orderby;
}
}提示錯(cuò)誤:Warning: Missing argument 2 for wpdb::prepare(),$keyword = $wpdb->prepare($_REQUEST['s']);就是這一行。
1 回答

胡子哥哥
TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
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);
這是相關(guān)性排序吧?
跟lz說(shuō)下,wp還對(duì)$_REQUEST['s']進(jìn)行了一些處理.
比如test1 test2
會(huì)分成
$wpdb->posts.post_title LIKE '%test1%' and $wpdb->posts.post_title LIKE '%test2%'
其實(shí)還會(huì)search content,略過(guò)
添加回答
舉報(bào)
0/150
提交
取消