Assign 4 digit sequential member number to role upon registration (not $user_id)

WordPress 10 posts 2 voices

  1. I am working on a membership site with wordpress, members, buddypress, acf pro, and a custom post type.

    All users will have a buddypress profile. However, a custom wordpress role (Members plugin) will have access to post to the custom post type. I already have a custom post type setup (CPTUI plugin).

    Currently, when a user signs up through the buddypress registration form and they clicks the “I am an artist” radio button, they are automatically assigned the wp role named “artist”.

    Upon registration, I need to generate a sequential membership number starting at 0001 for users with the artist role that will be displayed on the front-end custom post type, which is their artist profile. I may need to display the number in other places as well.

    This member number needs to last forever and not be affected if a user or profile is deleted. In other words, if a user is removed we don’t want everyone else’s number to change.

    WordPress user ID will not work for several reasons. Primarily, because it is associated with all users and the signups will affect the sequential order of the artist numbers. It is important that artist numbers start at 0001 and increment up from there.

    I have done a lot of research and posted in other forums. I got some help, but I feel stuck. This one simple thing is holding up the whole project. I need to get this part working before I can finish the rest of the website build because of this requirement.

    I am not here empty handed though. I think I almost have a good solution, but something is still not working. I am posting here in hopes that you can see what I cannot.

    I have this code in my functions.php file:

    function add_member_number( $user_id ) {
    // Get the last user with member_number meta key
    $args = array(
      'role'         => 'artists',
      'meta_key'     => 'member_number',
      'meta_compare' => 'EXISTS',
      'orderby'      => 'id',
      'order'        => 'DESC',
      'number'       => '1',
      'fields'       => 'ids',
    $users_array = get_users( $args );
    // Select a single user into a separate array to ease the process below.
    $user = array_pop( $users_array );
    // Get member_number meta value for this user
    $member_number = get_user_meta( $user['id'], 'member_number', true );
    // Increment the last member number.
    $number = $member_number++;
    // Save new number for newly registered user.
    update_user_meta( $user_id, 'member_number', $number );

    However, nothing gets written to the user meta in the database! If I hard code a number in place of $number (see below), the hard coded number gets written to the database. So something isn’t quite working with $number.

    // Save new number for newly registered user.
    update_user_meta( $user_id, 'member_number', 0001);

    What am I missing here? I appreciate any assistance you can offer. I am eager to learn.

  2. Actually, the member number can start at “1”. It does not have to be 4 digits.

  3. I’ve been giving this some thought over the past few days. Here’s a rough draft of what I came up with. It’s a set of functions for handling the functionality that you need (untested code).

    You can change up how things are named if needed. The raja_update_member() function at the bottom is what you’d want to call when updating the user (on registration, etc.).

     * Gets a user's artist number.
    function raja_get_user_artist_number( $user_id ) {
    	return get_user_meta( $user_id, 'artist_number', true );
     * Sets a user's artist number.
    function raja_set_user_artist_number( $user_id, $number ) {
    	return update_user_meta( $user_id, 'artist_number', $number );
     * Gets the last artist number from the DB.
    function raja_get_last_artist_number() {
    	return get_option( 'raja_last_artist_number', 0000 );
     * Stores the last artist number in the DB.
    function raja_set_last_artist_number( $number ) {
    	return update_option( 'raja_last_artist_number', $number );
     * Generates a new artist number.  We add +1 to the last number stored in the DB.
    function raja_generate_new_artist_number() {
    	$last_number = raja_get_last_artist_number();
    	// The '%04d' makes sure we have a 4-digit number.
    	return sprintf( '%04d', absint( $last_number ) + 1 );
     * Function to use when updating a member.  This can be called on registration.
    function raja_update_member( $user_id ) {
    	$has_number = raja_get_user_artist_number( $user_id );
    	// If the user already has a number, bail.
    	if ( $has_number )
    	// Generate a new artist number.
    	$number = raja_generate_new_artist_number();
    	// Set the user's artist number.
    	raja_set_user_artist_number( $user_id, $number );
    	// Store the last artist's number in the database.
    	raja_set_last_artist_number( $number );
  4. Justin,

    This code seems to be working great! However, I need the member number to start at “1” or “0001”. So adding it to the wp user_id is not viable.

  5. The user ID is only used to add as post meta to the user in the code above. I could be missing something though.

  6. When I registered two new users to test it, user id +1000 is what is placed in the $unique_id in the user_meta database. Shoulid I be looking for something else?

  7. I thought artist_number would be in the user_meta database, but it is not being created and logged.

  8. The code I posted doesn’t do anything on its own. It does, however, work. I just gave it a test.

    For testing, I added the raja_update_member() function to user_register like so:

    add_action( 'user_register', 'raja_update_member' );

    For testing, I simply added a couple of new users. In the wp_option table, you should have a raja_last_artist_number option that tracks the last number.

    In the wp_usermeta table, you should see the artist_number meta key with the correct meta value.

    Of course, you’ll want to run a role check and so on to make sure the user has the role and any other conditionals needed. The above code is just the functionality for handling the feature.

  9. I’m running on user_register, which is the hook used in WP. I’m not sure if BuddyPress does its own thing (I don’t use that plugin). You’ll want to check to see if there’s another hook for BuddyPress.