【问题标题】:Retrieve all users from wordpress database via REST/JSON API通过 REST/JSON API 从 wordpress 数据库中检索所有用户
【发布时间】:2023-03-25 05:05:02
【问题描述】:

首先:我正在开发一个需要使用 Wordpress 注册的 iOS 和 Android 应用。

我正在使用 WP REST API v2,但我读到它只检索为博客发过帖子的用户。

如何检索注册到我的 Wordpress 博客的所有用户的列表?即使是那些从未写过帖子的人?

感谢您的帮助!

【问题讨论】:

    标签: android ios wordpress swift rest


    【解决方案1】:

    您可以获取所有未创建帖子的用户,为此您需要修改rest-api插件。

    打开wp-content/plugins/rest-api/lib/endpoints/class-wp-rest-users-controller.php文件,你会在第106行找到下面的代码,

    if ( ! current_user_can( 'list_users' ) ) {
        $prepared_args['has_published_posts'] = true;
    }
    

    改成下面,

    if ( ! current_user_can( 'list_users' ) ) {
        $prepared_args['has_published_posts'] = false;
    }
    

    如果您不想修改插件,请将以下代码放入当前主题的functions.php 文件中。

    add_filter( 'rest_user_query' , 'custom_rest_user_query' );
    function custom_rest_user_query( $prepared_args, $request = null ) {
      unset($prepared_args['has_published_posts']);
      return $prepared_args;
    }
    

    你已经完成了。

    【讨论】:

    • 因为我的托管,我选择修改api文件。非常感谢。我试过了,效果很好!你拯救了我的一天^_^
    猜你喜欢
    • 2020-11-11
    • 1970-01-01
    • 2022-08-17
    • 2014-10-31
    • 2021-11-02
    • 2021-09-08
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多