I need your help, I am really desperate.

In the beginning whenever a user is registered I create category with name based on that user's team name. That user also gets a unique custom role 'registered-team'. This code works:

add_action( 'user_register', 'insert_category', 10, 1 );
function insert_category( $user_id ) {

$teamname = $_POST["teamname-380"]; 
$username = $_POST["user_login-380"];

    if(!term_exists('Posts by ' . $teamname)) {
            'Posts by ' . $teamname,
              'description' => "Posts by " . $teamname ,
              'slug'        => $username
  1. But after that I want to automatically assign category of every post posted by user of role 'registered-team'. I tried this code, but it does not work.

    add_action( 'save_post', 'check_cat_user' );
    function check_cat_user( $post_id ) {   
        $post_user_role = get_the_author_meta('role');
        if ($post_user_role == 'registered-team') {
            $post_username = get_the_author_meta('user_login');
            $post_teamname = get_the_author_meta('teamname');
            $des_category = "Posts by " . $post_teamname;
            wp_set_post_categories( $post_id, $des_category );
  2. Then I want to hide certain elements from edit/create post page. I want only title, editor, tags, featured image and publish to be edited by that user. How can I achieve this easily?

Thanks in advance!

  • To sandbox users it's a lot easier to create a custom post type for every permission set. So instead of creating a category for each team, I'd suggest a CPT for each team. You can then disable certain features for those post types if you like and that will help remove some of the other metaboxes they can see and edit. – WebElaine Feb 15 '18 at 19:35
  • The save_post hook is fired after the post is saved, try to use the wp_insert_post_data filter instead. – Stefano Tombolini Feb 16 '18 at 10:15

Your Answer

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

Browse other questions tagged or ask your own question.