Skip to content

Instantly share code, notes, and snippets.

@leftlane
Created June 12, 2012 06:37
Show Gist options
  • Save leftlane/2915638 to your computer and use it in GitHub Desktop.
Save leftlane/2915638 to your computer and use it in GitHub Desktop.
Add a meta_key to all WordPress users of Subscriber Role using add_user_meta
<?php // Create the WP_User_Query object
$wp_user_query = new WP_User_Query( array( 'role' => 'subscriber' ) );
// Get the users
$users = $wp_user_query->get_results();
// Check for users
if (!empty($users))
{
// Loop through each user
foreach ($users as $user)
{
// Add the meta_key and the value
add_user_meta($user->ID, 'name_of_new_meta_key', 'value_of_meta_key', false);
}
} else {
echo 'No users found.';
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment