Home Forums General Issues Set meta value for all users


Set meta value for all users

  • I have a custom boolean field that is defaulted to false. I am using it to mark users we manually add to our system as they get special privileges on the site. So when someone registers it will be set to false.

    The problem is I need to set it to true for all pre-existing members as all of them up until now have been manually added and it would be too time consuming to do it manually. How do I go about assigning this fields value to true all currently existing users ?

  • Hi @youngwolf0

    A very good question. To which I don’t have an easy solution.

    I would write a custom function (and run it once, then delete it) that loops through all your users (via a WP_User query), and for each user, use the update_field function (documented on this site) to save a value of 1 for the field.

    I hope this makes sense.


  • I wrote the following function and call and placed them in the active themes functions.php file:

    function set_legacy_members($members_value) {
        $members = get_users();
        $is_member = 'field_522f3c55f16be';
        foreach ($members as $member) {
        	$user = "user_" . $member->ID;
            update_field($is_member, $members_value, $user);

    However this hasn’t worked, the field is still set to false for every member, what am I doing wrong?

  • Looking in the wp_usermeta table as well only two members have a value and they are ones I set manually yesterday.

  • Hi @youngwolf0

    Your code looks perfect, so for that great work.

    Your next step is to debug the function to find out at what line it is failing.

    Firstly, turn on DEBUG MODE in your wp-config.php file and then use a simple script like so to test that your loop is working:

    $user = "user_" . $member->ID;
    echo '<pre>';
    	print_r( $user );
    echo '</pre>';
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Set meta value for all users’ is closed to new replies.