Some people want to show posts to only registered users in wordpress in site. You just need to create category and publish your posts in private category.
How to show posts to only registered users in wordpress
Use the following code in index.php and single.php and page.php file.
$cat_id = get_cat_ID('private'); $args=array( 'category__not_in' => $cat_id, 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5 ); if(!is_user_logged_in()){ $cat_id = get_cat_ID('private'); $args=array( 'category__not_in' => $cat_id, 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5 ); } else { $cat_id = get_cat_ID('public'); $args=array( 'category__not_in' => $cat_id, 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 5 ); } // The Query query_posts( $args ); // The Loop while ( have_posts() ) : the_post(); echo '<li>'; the_title(); echo '</li>'; endwhile; // Reset Query wp_reset_query();
Should I use the code in all the three pages like index.php, page.php and single.php? Isn’t there any way this can be achieved by a plugin so that we can allow it irrespective of theme?
You can use this code in any of pages.