您只能允许注册用户通过以下方式查看您的内容
1.此解决方案仅对登录用户进行后期显示
class RavsPublic {
function __construct() {
add_action(\'pre_get_posts\', array($this, \'try_redirect\'));
}
function try_redirect( $query ) {
// not on home page and not login
if( !is_home() && !is_user_logged_in() ){
// send them to home page
wp_redirect( home_url() );
exit();
return $query;
}
return $query;
}
}
new RavsPublic();
2)如果每个帖子都包含一个可以查看该帖子的用户列表(作为帖子元),则此解决方案。
//show your content only to registered users
class RavsPublic {
function __construct() {
add_action(\'pre_get_posts\', array($this, \'try_redirect\'));
}
function try_redirect( $query ) {
global $current_user;
get_currentuserinfo();
//current user login name
$user = $current_user->user_login;
$allowed_user_list = False;
//if visiter visiting a post
if( $query->query_vars->is_single ){
//retrieve post
$args=array(
\'name\' => $query->query_vars->name,
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 1
);
$my_posts = get_posts( $args );
//retrieve post meta which contain allowed user list
$allowed_user_list = get_post_meta( $my_posts[0]->ID, \'allowed_user_list\');
//check if user exist in allowed user list
$is_user_allowed = in_array( $user, $allowed_user_list );
}
//redirect visiter to home page if he\'s not allowed to see this post or not login
if( !is_home() && !is_user_logged_in() && !$is_user_allowed ){
wp_redirect( home_url() ); // doesn\'t work
exit();
return $query;
}
return $query;
}
}
new RavsPublic();