【发布时间】:2019-10-20 11:19:48
【问题描述】:
我想使用这个 SQL。
"ORDER BY CASE WHEN post_title >= 'C' THEN 1 ELSE 0 END DESC, post_title ASC".
此 SQL 对帖子进行排序并返回以字母“C”开头的帖子。我想通过首字母排序来搜索帖子。这个 SQL 代码是我的决定。
- 我尝试使用过滤器“posts_orderby”添加这部分 SQL。但这不起作用。
- 我试试 $wpq = new WP_Query($query);
$sql = "SELECT post_title FROM `baddoc_posts` WHERE post_title LIKE '%Иван%' AND post_type = 'doctor' ORDER BY CASE WHEN post_title >= 'И' THEN 1 ELSE 0 END DESC, post_title ASC";
$wpq = new WP_Query();
$wpq->parse_query($sql);
$wpq->get_posts();
function wpse_298888_posts_where( $where, $query ) {
global $wpdb;
$starts_with = $query->get( 'starts_with' );
if ( $starts_with ) {
$where .= " ORDER BY CASE WHEN $wpdb->posts.post_title >= '$starts_with' THEN 1 ELSE 0 END DESC, $wpdb->posts.post_title ASC";
}
return $where;
}
add_filter( 'posts_orderby', 'wpse_298888_posts_where', 10, 2 );
【问题讨论】: