Restrict access and display for categories

Is it possible to choose specific categories that will only be displayed to logged in members?

I tried to find a plugin or a PHP solution that does that but could not find anything that was still relevant.

Related posts

Leave a Reply

2 comments

  1. Yes :).

    The following will effect all queries for posts on the front-end including ‘secondary loops’ (e.g. post lists on the side) but it won’t effect custom post types. So this may not be exactly what you’re after, but the principle is the same:

    add_action('pre_get_posts','wpse72569_maybe_filter_out_category');
    
    function wpse72569_maybe_filter_out_category( $query ){
    
         //Don't touch the admin
         if( is_admin() )
             return;
    
         //Leave logged in users alone
         if( is_user_logged_in() )
             return;
    
         //Only effect 'post' queries
         $post_types = $query->get('post_type');
         if( !empty($post_types) && 'post' != $post_types )
             return;
    
         //Get current tax query
         $tax_query = $query->get('tax_query');
    
         //Return only posts which are not in category with ID 1
         $tax_query[] = array(
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => array( 1 ),
            'operator' => 'NOT IN'
              );
    
         //If this is 'OR', then our tax query above might be ignored.
         $tax_query['relation'] = 'AND';
    
         $query->set('tax_query',$tax_query);
    
    }
    
  2. You could try this if your looking to use it a loop type structure:

    <?php
      if ( is_user_logged_in() ) {
    
         //Only displays if the user is logged in.
    
         query_posts('cat=1'); // Replace the category number with the correct category number you want to display
          while (have_posts()) : the_post();
          the_content();
         endwhile;
    
     } else {
    
         // Everyone else that is not logged in
    
         query_posts('cat=2'); // Replace the category number with the correct category number you want to display
           while (have_posts()) : the_post();
           the_content();
        endwhile;
    
    }
    ?> 
    

    If you are looking to just display links to the category pages like a menu you can do this:

    1. Use the WP nav menu feature. Register the menu in your functions.php
      in your theme folder. You can do this by adding this to the file:

      <?php
      
        register_nav_menus( array(
           'primary' => __( 'Primary Navigation', 'your_theme_name' ), // Your main navigation. Replace "your_theme_name" with the theme you are using.
           'logged_in_user' => __( 'Logged In User', 'your_theme_name' ), // Remember replace "your_theme_name" with the theme you are using.
      
      ) );
      ?>
      
    2. Add the categories to the menu via the administration.

    3. Call it in your theme. This is similar to what we have done above but will just list it like a menu and not a post listing.

      <?php
         if ( is_user_logged_in() ) {
      
          //Only displays if the user is logged in.
      
           wp_nav_menu( array( 'container' => false, 'menu_id' => 'nav', 'theme_location' => 'logged_in_user' ) );
      
         } else {
      
        // Everyone else that is not logged in
       // You can display anything you want to none logged in users or just leave it blank to display nothing.
      
        }
       ?>