【问题标题】:Wordpress rest api return users who have not created a postWordpress rest api 返回未创建帖子的用户
【发布时间】:2019-04-24 03:14:05
【问题描述】:

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

【问题讨论】:

    标签: wordpress wordpress-rest-api


    【解决方案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

    【讨论】:

    • @Ruben Pauwels 当我添加此代码时,根本不会返回任何用户。
    猜你喜欢
    • 2021-10-07
    • 2016-06-14
    • 2017-04-21
    • 1970-01-01
    • 2018-04-05
    • 2020-01-09
    • 2022-11-27
    • 1970-01-01
    • 2021-02-04
    相关资源
    最近更新 更多