Restrict guest to view only blog posts

Restrict guest to view only blog posts, instead of all public posts.

blog 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' );

41 total views, 1 views today

Author: Albert

Leave a Reply