【问题标题】:Is there a way to get pending comments/posts from wordpress api?有没有办法从 wordpress api 获取待处理的评论/帖子?
【发布时间】:2020-02-23 02:07:02
【问题描述】:

我正在尝试从 wordpress API 获取待处理的 cmets/posts。 当我输入这个http://testWeb/wp-json/wp/v2/comments 时,它只返回已发布的项目。有没有办法获得待处理的项目?我正在尝试做的是在我的应用中从待处理更改为已发布。

编辑: 我已通过身份验证 我在wordpress的functions.php中添加了以下代码:

wp_enqueue_script('custom_js',get_template_directory_uri() . '/js/custom.js', NULL, 1.0, true);
    wp_localize_script('custom_js', 'magicalData',array(
        'nonce' => wp_create_nonce('wp_rest'),
    ));

在我的 custom.js 中,我有:

fetch("http://localhost/testWeb/wp-json/wp/v2/comments?status=pending", {
  method: "GET",
  headers: {
    "Content-Type": "application/json; charset=UTF-8",
    "X-WP-Nonce": magicalData.nonce  //authorization 
  }
})
  .then(res => res.json())
  .then(data => console.log(data));  // t

我仍然一无所获(空对象,尽管我在 wordpress 面板中有一条待处理的评论)

【问题讨论】:

  • 你试过/cmets?status=pending
  • 不,它不起作用。当我查询时:/cmets?status=pending 我得到这个响应:{“code”:“rest_forbidden_​​param”,“message”:“查询参数不允许:状态", "数据": { "状态": 401 } }
  • 状态是“持有”而不是“待定”

标签: javascript wordpress wordpress-rest-api


【解决方案1】:

首先为给定用户设置应用程序密码。编辑用户以执行此操作

然后使用此调用,其中 USERNAME 是用户名,而不是应用程序名称

$ curl --user "USERNAME:APPLICATION_PASSWORD" https://example.com/wp-json/wp/v2/comments?status=hold

【讨论】:

    【解决方案2】:

    您必须通过身份验证才能访问待处理的 cmets

    https://developer.wordpress.org/rest-api/reference/comments/

    【讨论】:

      猜你喜欢
      • 2019-07-30
      • 2011-06-17
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 2014-02-28
      相关资源
      最近更新 更多