Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:10:35+00:00 2026-06-07T21:10:35+00:00

Since almost 4-5 months I am trying to solve this issue where user role

  • 0

Since almost 4-5 months I am trying to solve this issue where user role automatic change with certain number of post count. Say if x user have <= 10 post he/she is in contributor role if <=20 author role so on..

With help of you all people I could achieve this code.

<?php

add_action( 'save_post', 'update_roles' );
    function update_roles( $post_id ) {

        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id;

        // Get the author
        $author = wp_get_current_user();

        // Set variables for current user information
        $count = count_user_posts( $author->ID );
        $current_role = (array) get_user_meta( $author->ID, 'wp_capabilities' );

        get_currentuserinfo(); 
        global $user_level;

        // Do the checks to see if they have the roles and if not update them.
        if ( ($user_level != 10) && ( $count > 3 && $count <= 5 ) && ( array_key_exists( 'contributor', $current_role[0] ) ) ) {
            $user_id_role = new WP_User( $author->ID );
            $user_id_role->set_role( 'author' );

        } elseif ( ($user_level != 10) && ( $count > 5 && $count <= 9 ) && ( array_key_exists( 'author', $current_role[0] ) ) ) {

            $user_id_role = new WP_User( $author->ID );
            $user_id_role->set_role( 'editor' );

        } elseif ($user_level != 10){

            $user_id_role = new WP_User( $author->ID );
            $user_id_role->set_role( 'contributor' );

        } return $post_id;

    }

?>

This code working some time and some time doesnt reacting at all. This code is buggy and if once somehow will auto change the role and if I change role again back to down side than it will never auto change as per post count. same if I delete post than its not affecting at all. However it should always consider the post count and imminently should effect with the number of post.

I am really getting tired with this but my entire site is based on this code only. So I really need you great people help.

Thanks a lot lot lot..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:10:37+00:00Added an answer on June 7, 2026 at 9:10 pm

    what i think is there is a mistake in starting users role because:

    if ( ($user_level != 10) && ( $count > 3 && $count <= 5 ) && ( array_key_exists(     'contributor', $current_role[0] ) ) ) {
            $user_id_role = new WP_User( $author->ID );
            $user_id_role->set_role( 'author' );
    

    If user level is 10 AND post count is major than 3 AND minor or equal than 5 set user role to Author

    but in the second

    } elseif ( ($user_level != 10) && ( $count > 5 && $count <= 9 ) && ( array_key_exists( 'author', $current_role[0] ) ) ) {
    
            $user_id_role = new WP_User( $author->ID );
            $user_id_role->set_role( 'editor' );
    

    You say again that conditional tag starts only IF THE USER ROLE IS 10… so, for example, if users have posted 4 posts, and it’s role is author the conditional tag will not works

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since I am developing an iOS >= 5.0 application, I am trying to change
So I'm googling this question and can't really find any clear examples since almost
I've HTML5 version of facebook like button on my site since almost an year
Since I have no errors I don't know if this is the right place
I have been working and debugging with NetBeans since almost a year with 0
Today my application threw an OutOfMemoryException . To me this was always almost impossible
I've been stuck trying to write this regular expression I need. Basically, I have
This was posted 8 months ago: Codeigniter 2.0 third_party folder . This was posted
It's almost a year ago that I asked this question on SO: Menu Accelerator
I am trying to figure out how to tackle this problem. I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.