0

I've been able to successfully empty the meta_value for a specific meta_key. The problem is that a new meta_key will be created for all users in the database. I do not want all users to have the meta_key created in the database unless it already exist.

I think perhaps if I set 'guest_list_venue_names' as a variable and check if it exist I can use an IF statement? I'm new to PHP so any insight would be helpful.

$wp_user_query = new WP_User_Query(array('role' => 'Subscriber'));

$users = $wp_user_query->get_results();

if (!empty($users)) {


    foreach ($users as $user)
    {
        update_user_meta( $user->id, 'guest_list_venue', ''); 
        update_user_meta( $user->id, 'guest_list_venue_names', ''); 
    }
}

2 Answers 2

0

You could specify the meta key in the user query so the results you're getting back are only for users who have an existing field.

For reference WP_User_Query custom field(user meta) params.

$args = array(
    'role' => 'Subscriber',
    'meta_query' => array(
        array(
            'key' => 'old_meta_key_name',
            'compare' => 'EXISTS'
        )
    ),
);
$users_with_meta = new WP_User_Query( $args );
$users = $users_with_meta->get_results();
1
  • This is great! Works exactly how I need it. Thanks!
    – Rob
    Commented Oct 21, 2022 at 17:13
0

my friend. You should use the get_user_meta() function to check if there is meta in the user table or not. If there is, replace the meta value.

$meta = get_user_meta($user->id,
'guest_list_venue')
if(! empty($meta)){
   update_user_meta( $user->id, 'guest_list_venue', 'value');
}
2
  • This is absolutely useful for both my question and also the second part of my project which is just as important. I can use it to clear the meta_value with a button click. Thank you very much!
    – Rob
    Commented Oct 21, 2022 at 17:20
  • Hello. I am glad that I could help you. Commented Oct 21, 2022 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.