我想按特定名称对作者进行排序。
这是我的代码:
$authorname = 'max';
$blog_arr = array(
'posts_per_page' => -1,
'post_type' => 'post',
'orderby' => '(author = "'.$authorname.'") desc'
);
$post_arr = get_posts($blog_arr);
不幸的是,WordPress在核心中过滤了我的订单。
这个答案也无济于事: SQL Server - Order by case, Ordering by specific field value first,How do I return rows with a specific value first?
谢谢你的帮助。 最好的问候
答案 0 :(得分:0)
我找到了一个有效的解决方案:
function get_user_posts() {
$blog_arr = array(
'posts_per_page' => -1,
'post_type' => 'post'
);
global $sc_user;
$authorname = 'max';
$sc_user = get_user_by('login', $authorname);
add_filter('posts_join_request','edit_posts_join_request');
add_filter('posts_orderby', 'edit_posts_orderby');
$post_arr = get_posts($blog_arr);
remove_filter('posts_join_request','edit_posts_join_request');
remove_filter('posts_orderby', 'edit_posts_orderby');
return $post_arr;
}
function edit_posts_join_request($join_request_statement) {
$join_request_statement .= "join wp_users ON wp_posts.post_author = wp_users.ID";
return $join_request_statement;
}
function edit_posts_orderby($orderby_statement) {
global $sc_user;
$orderby_statement = 'CASE WHEN wp_users.user_login = "'.$sc_user->user_login.'" THEN 0 ELSE 1 END, wp_users.user_login';
return $orderby_statement;
}