【问题标题】:User allow to create/update Only Own Post用户只允许创建/更新自己的帖子
【发布时间】:2018-11-28 22:37:27
【问题描述】:

我创建了一个自定义帖子类型(即公司资料),我希望允许用户仅创建或更新他们拥有的资料。我该怎么做?

还有一个钩子或方法可以检查以确定用户是否是内容的作者,如果用户创建了内容然后重定向到编辑配置文件。

【问题讨论】:

  • 创建一个不能编辑其他帖子的用户角色。或检查哪些用户角色已经拥有它。
  • 我已经编辑了文本,所以它在英文中更有意义。

标签: wordpress


【解决方案1】:

这个功能或许能帮到你。

它检查用户是否可以编辑其他人的帖子,

如果他们不能,只在仪表板中显示他/她自己的帖子。

/*only allow editors and admin to see all posts.*/
function posts_for_current_author($query) {
    global $pagenow;
    if( 'edit.php' != $pagenow || !$query->is_admin )
        return $query;
        if( !current_user_can( 'edit_others_posts' ) ) {
            global $user_ID;
            $query->set('author', $user_ID );
        }
    return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

您可以使用类似的方法从用户配置文件重定向。

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2022-10-02
    • 2019-10-30
    相关资源
    最近更新 更多