admin管理员组文章数量:1407738
I am tring to build a point system for users. To save points I thought to use user meta. So I need to create a meta user called poins for all cureent users as well as futures users.
add_user_meta function need a user id. So will I have to use a loop?
I am tring to build a point system for users. To save points I thought to use user meta. So I need to create a meta user called poins for all cureent users as well as futures users.
add_user_meta function need a user id. So will I have to use a loop?
Share Improve this question asked Apr 6, 2016 at 1:21 SuganthaSugantha 471 silver badge4 bronze badges1 Answer
Reset to default 5yes, you would have to loop through all
users so that you could then update that user_meta field:
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query(array('role' => 'Subscriber'));
// Get the results
$users = $wp_user_query->get_results();
// Check for results
if (!empty($users)) {
// loop trough each author
foreach ($users as $user)
{
// add points meta all the user's data
add_user_meta( $user->id, 'points', '5', true ); // 5 = number of points existing users will get
}
}
as far as adding this same user_meta to new users you could hook into the login or registration, depending on how you are adding them to the system. for this you could use get_current_user_id()
to get the id of that unique user.
later, you could then just update_user_meta($user_id,'points');
in whatever function they are earning more points at.
you can find more info on the WP_User_Query in the Codex
本文标签: How to add user meta for all users
版权声明:本文标题:How to add user meta for all users 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744662186a2618310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论