WordPress 按特定作者姓名排序

WorsPress orederby specific author name

我想按具体姓名对作者进行排序。

这是我的代码:

$authorname = 'max';

$blog_arr = array(
    'posts_per_page' => -1,
    'post_type' => 'post',
    'orderby' => '(author = "'.$authorname.'") desc'
);

$post_arr = get_posts($blog_arr);

不幸的是,WordPress 在核心中过滤了我的 orderby。

这个答案也没有帮助: SQL Server - Order by case , Ordering by specific field value first, How do I return rows with a specific value first?

感谢您的帮助。 最好的问候

我找到了可行的解决方案:

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;
}