Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

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

}
share|improve this answer
    
Thanks for this, I've tested it and it does not work.. I'm only using classic posts and I've added the correct category id to the array and added this whole piece of code to the functions.php file but still I'm able to see posts from that category even when I'm not logged in. –  user838437 Nov 15 '12 at 9:15
    
Yup, the main query isn't for 'posts' - its usually empty. I've made some changes - so it should now work - but you may want to be careful about which queries you are affecting. You might be able to remove the post types check completely. –  Stephen Harris Nov 15 '12 at 9:39
    
This works great now! Just one more question, is it possible to edit this so it would hide the selected categories from menus as well? –  user838437 Nov 15 '12 at 9:50
add comment

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.
    
      }
     ?> 
    
share|improve this answer
    
And user will get pagination 404 problem.... –  Oleg Butuzov Nov 14 '12 at 15:37
    
Why would they get a 404? I have used this numerous times and have not gotten a 404 when adding pagination to the code. –  Nicole Nov 14 '12 at 15:46
    
Because going to page 2 will be page 2 of the original query (which may not exist) - and not page 2 of the query in query_post(). Don't use query posts :) –  Stephen Harris Nov 14 '12 at 17:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.