0

I have to make a site where i have to assign posts to users.

Want i already did:

1: made new user roles with only 'read' enabled

But i can't seem to figure out how to assign a certain post to a specific user/user role. So that when there logged in they will only see the post that is assigned to them, instead of seeing all the post and when they click it they get a message like: "Sorry, you have to be user*** to see this post."

Does anyone know how to do this?

1

You search certainly something like Members plugin.

It allow you create a custom Role and You can make your site and its feed completely private if you want or just one or 2 posts. Its like you want.

Or Manually, you can add new roles:

function add_role() {
    add_role( 'private_user', "private user",array(
        'is_able_to_read_private_page'=> true,
        'read'=> true
    ) );
}
add_action('init','add_role');

And Create a custom page and using something like: (ITS AN EXAMPLE)

 <?php /*Template Name: Private page Template*/
 get_header();
 if(current_user_can( "is_able_to_read_private_page")):
    if( have_posts() ):
       while( have_posts() ):
           the_post();
        endwhile;
    endif;
 else:
     echo "Access Denied";
 endif;
 get_footer();?>
  • This is good if the post your are trying to make private is the same for every user, but you cannot filter on a per user basis – bynicolas Aug 10 '16 at 15:31
  • per user.. you can add a user_meta_data and play with it... but I think its not the question. he said specific user/user role. User role its what I describe in my post. Specific user, it can add it into the user role. To get better handle to his user, he can use user_meta_data and proceed like I said – Jean-philippe Emond Aug 10 '16 at 15:35
  • I took the second part of that sentence they will only see the post that is assigned to them In that case, you need to assign the post to a specific user and that assignment can be done by setting the post author to the user id. I posted an answer covering that but assigning a meta value to the user would be a good way to approach this too. I guess the context was not clear enough in the question. – bynicolas Aug 10 '16 at 19:35
1

I did something similar once.

I needed to attach one or more CPT to specific customers.

I made a searchable select box containing all users in the edit screen of the CPT only available to admins.

The admin would create the post, save it, and then change the author of the post to the desired customer username.

The reason I chose to go this way is because a post cannot assigned to a user, unless that user is it's author. You could also try to add a metabox to your post type which will contain the authorized users for your post to filter by meta key. But by using this method you can easily filter using the main query. It's just a matter of changing the ownership of your post. I guess it all depends on the nature of your project.

Then I had a script which filtered the posts something like this.

Note that this code is a general idea. Writing from what I remember.

add_action( 'pre_get_posts', 'wpse_show_user_posts');
function wpse_show_user_posts( $query ){

  // Don't filter if user is an administrator
  if ( current_user_can( 'list_users' ) )
    return;

  // Get all posts which our current user is an author for
  if( is_user_logged_in() && $query->is_main_query() ){

    $current_user = wp_get_current_user();

    $query->set( 'author', $current_user->ID );

  }

  // Hide all posts otherwise
  if( $query->is_main_query() ) {

    $query->set( 'category__not_in', '1' ); // Use the id of the retricted category

  }

  return $query;

}

Of course you might want to use custom roles with appropriate capabilities so you have better control on what your users can do. Know that any users can be the post authors, but the default author metabox select field on a post edit screen will only return users with at least contributor roles (which also have delete_posts capability). So that is why I created a custom select box returning the users from my custom role.

You might also only return that restricted content only to logged in users.

add_filter( 'the_content', 'logged_in_only' );
function logged_in_only( $content ){
  $categories = get_the_category();
  $cat_not_in = 'some-cat';
  $include_post =  true;

  foreach( $categories as $category ){
    if( $category->slug == $cat_not_in )
      $include_post = false;
  }

  if( is_user_logged_in() || $include_post ) {
    return $content;
  }

  return 'You need to log in <a href="' . home_url( 'wp-login.php') . '">Here</a>';
}

You would need to create a restricted category and assign the posts you wish to control to that category in order to properly filter those posts to users not logged in.

  • Do i have to make a custom page-{post_type}.php for this and than put it in there? Or do i have to put this in the functions.php? – Dionoh Aug 11 '16 at 8:46
  • This will work if you put it functions.php because we are hooking into filters. page-{post_type}.php are templates that will automatically load based on template hierarchy. But like I said, it's mainly a concept to illustrate what can be done. My answer still lack the code to change a post author who's role is not contributor or higher. If a post needs to be read by only one user, then my answer is the way to go, if the same post can be read by users in a given group, capabilities can easily help you there. – bynicolas Aug 12 '16 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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