Restrict guest to view only blog posts, instead of all public posts.
1. Add 'blog' tag to all blog posts.
2. Add pre_get_posts action to restrict the query.
function show_only_blog( $query ) {
if ( is_user_logged_in() ) return;
$tax_query = array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => 'blog',
),
);
$query->set( 'tax_query', $tax_query );
}
add_action( 'pre_get_posts', 'show_only_blog' );
Comments