0

I want to add a custom meta data for all the users with a default false value. I have researched about adding a meta data i.e, add_user_meta( int $user_id, string $meta_key, mixed $meta_value, bool $unique = false ) which allows only adding a meta data to a particular user. But I want to add it for all the users. How can I achieve this? Thanks.

0

Adding custom field to all existing users.

You will have to get the list of users and then, in a loop, set a custom field for each of them.

$users = get_users( [
    'fields' => 'id', 
    'meta_key' => 'YOUR_META_KEY', 
    'meta_compare' => 'NOT EXISTS'
] );
if ( is_array($users) && count($users) )
{
    $meta_value = false;
    foreach( $users as $user_id ) {
        add_user_meta( $user_id, 'YOUR_META_KEY', $meta_value, true);
    }
}

The version with a single query requires the use of an SQL query. The function will return FALSE in case of error.

global $wpdb;
$sql_query = "INSERT INTO {$wpdb->usermeta} (user_id, meta_key, meta_value) "
    . "SELECT ID, 'YOUR_META_KEY', '' FROM {$wpdb->users} u "
    . " LEFT JOIN {$wpdb->usermeta} um ON um.user_id = u.ID AND um.meta_key='YOUR_META_KEY' "
    . " WHERE um.umeta_id IS NULL";
$result = $wpdb->query( $sql_query );

Above code should be run once.

Example:
functions.php

add_action( 'init', 'se338136_add_meta_to_existing_users' );
function se338136_add_meta_to_existing_users() {
    $option_meta_name = '_my_meta_added';
    $executed = get_option( $option_meta_name, false );
    if ( $executed == 1 )
        return;

    // here one of the above codes

    // if ($result !== false )
    update_option( $option_meta_name, 1 );
}

Adding a custom field when adding a new user.

add_filter( 'insert_user_meta', 'new_user_meta', 20, 3);

/**
 * @param array   $meta   Default meta values and keys for the user.
 * @param WP_User $user   User object.
 * @param bool    $update Whether the user is being updated rather than created.
 */
function new_user_meta( $meta, $user, $update ) 
{
    if ( $update )
        return $meta;

    $meta['YOUR_META_KEY'] = false;
    return $meta;
}

References:

  • Hi, how can i add the user meta for a newly created user. Will this code work for all the users if they are new created also? – Subham May 18 at 4:52
  • This is the code for a single run, add a metadata for all existing users, then it should be removed. That's how I understood: I want to add it for all the users.. To add a custom field when creating a new user, use the insert_user_meta filter hook. – nmr May 18 at 5:18
  • Thats's great. I'll try that and get back to you. – Subham May 18 at 5:20
  • @Subham I added a variant for creating a new user with a custom field – nmr May 18 at 5:29
  • So, if i add this code alone it should work right? – Subham May 18 at 5:40

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.