【问题标题】:how should i get all posts for particular user role in wordpress through plug-in我应该如何通过插件在wordpress中获取特定用户角色的所有帖子
【发布时间】:2015-02-11 07:27:58
【问题描述】:

我正在使用 wordpress 数据库作为 android 应用程序的后端,我在应用程序中有 2 个自定义角色,一个是管理,第二个是用户。我在 wordpress 中使用成员插件来获取内容权限,它允许添加权限。现在从我的插件我想为具有管理角色的用户获取所有帖子。这些请求将作为基于 REST 的 URL 并返回 json 数据。

【问题讨论】:

  • 能否重新表述一下你的问题,你的问题不清楚理解!
  • 已编辑问题。

标签: wordpress


【解决方案1】:

试试这个

创建一个函数来改变查询的 where 子句:

function authors_where_filter( $where ) {
        global $wpdb;
        $ids = get_users(array('role' => 'author' ,'fields' => 'ID'));
        $where .= " AND post_author IN ($ids)";
        return $where;
}

然后在查询之前将其挂钩:

add_filter('posts_where','authors_where_filter');
$all_posts = new WP_Query(array('posts_per_page' => -1 .....
remove_filter('posts_where');

您应该在一个查询中获取作者用户的所有帖子,(实际上两个是获取用户,另一个是获取帖子)

Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2019-03-26
    • 2014-01-25
    • 2016-08-11
    • 2020-04-09
    • 1970-01-01
    相关资源
    最近更新 更多