This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Show only when logged in Option

#37341
Ernest MarcinkoErnest Marcinko
Keymaster

Hi!

Well, there is no such option, but there is a programmatical way to exclude posts/pages/other cpt by their IDs, when the user is not logged in.

Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

You can add the unwanted post IDs to the $ids array variable.

add_filter( 'asp_query_args', 'asp_exclude_only_post_ids', 10, 2 );
function asp_exclude_only_post_ids( $args, $id ) {
  /**
   * Array of post IDs to exclude for non-logged in users
   */     
  $ids = array(1, 2, 3);
  
  // -- !! Do not change anything below this line !! --
  if ( !is_user_logged_in() ) {
	$args['post_not_in'] = array_merge($args['post_not_in'], $ids);
  }

  return $args;
}