在wordpress中仅显示当前用户的帖子

时间:2018-03-02 15:19:01

标签: wordpress

我正在尝试仅显示当前用户在前端创建的帖子。也就是说,当前登录的用户应该只能查看他/她在网站上创建的帖子,包括在博客档案中我想只显示当前用户的帖子。任何有链接的人都应该能够查看帖子。 我尝试了下面的代码,但这也限制了对当前用户没有创建的页面的访问。我只需限制访问帖子。

function exclude_other_users_posts( $query ) {
    if( !is_user_logged_in() ) {
        // guests cannot read private posts
        // and we exclude all public posts here
        // so guests can read nothing :-)
        $query->set( 'post_status', 'private' );
        $query->set( 'perm', 'readable' );
    } elseif( !current_user_can('read_others_posts') )
        $query->set( 'author', get_current_user_id() );
}
add_action( 'pre_get_posts', 'exclude_other_users_posts' );

1 个答案:

答案 0 :(得分:0)

您可以在要显示帖子的php文件中使用类似的内容。

global $current_user;
    get_currentuserinfo();
    $authorID = $current_user->ID;
        $args = array(
                  'post_type' => 'post',
                  'posts_per_page' => -1,
                  'author' => $authorID
                  );

        $wp_query = new WP_Query($args);

然后你可以让你的循环显示你的帖子。

相关问题