WordPress REST API返回尚未创建帖子的用户

时间:2018-11-21 14:11:48

标签: wordpress wordpress-rest-api

使用wordpress rest api,有一种方法可以返回所有用户,无论他们是否创建了帖子。我知道,如果以管理员用户身份进行身份验证,我可以这样做,但是我需要能够以标准用户(订阅者)身份进行操作

1 个答案:

答案 0 :(得分:1)

您可以通过在functions.php文件中添加此过滤器来实现。

add_filter( 'rest_user_query', 'prefix_remove_has_published_posts_from_wp_api_user_query', 10, 2 );
/**
 * Removes `has_published_posts` from the query args so even users who have not
 * published content are returned by the request.
 *
 * @see https://developer.wordpress.org/reference/classes/wp_user_query/
 *
 * @param array           $prepared_args Array of arguments for WP_User_Query.
 * @param WP_REST_Request $request       The current request.
 *
 * @return array
 */
function prefix_remove_has_published_posts_from_wp_api_user_query( $prepared_args, $request ) {
    unset( $prepared_args['has_published_posts'] );

    return $prepared_args;
}

来源:WP Users Endpoint doesn't return all users