dreljie602951 2014-06-19 08:45
浏览 104
已采纳

使用get_user_meta时WP中导致此错误的原因是什么?

I'm adding additional metadata to users in WordPress and every time I use get_user_meta I'm getting this error: Notice: Object of class WP_User could not be converted to int in /.../wp-includes/functions.php on line 2999

My code is

foreach( $metadata as $key => $value ) {

        if ( !get_user_meta( $wcm_users[$i], $key, '' ) ) {

            add_user_meta( $wcm_users[$i]->ID, $key, '' );

        }

    }

$metadata is an array of additional metadata I want to add, so its a basic check to see if the metadata is already added, if not, add in. Can't understand what's triggering the error. If I remove the get_user_meta the error goes away.

Any ideas of what is going on?

  • 写回答

1条回答 默认 最新

  • dsfovbm931034814 2014-06-19 08:48
    关注

    get_user_meta() takes an integer ID. You're passing it a WP_User object. Try:

        foreach( $metadata as $key => $value ) {
    
            if ( !get_user_meta( $wcm_users[$i]->ID, $key, '' ) ) {
    
                add_user_meta( $wcm_users[$i]->ID, $key, '' );
    
            }
    
        }
    

    Also, note that the third (optional) parameter to get_user_meta() is a boolean to indicate if you want to treat the value as a single value or an array—I'm guessing you should just be leaving it off to get the default behaviour of a single value. That's what the empty string you're passing will be doing (as an empty string is a boolean false) but it's not that obvious from your code.

    Also, update_user_meta() will add the meta if it doesn't exist, and update it if it does, which is what you say you want to do, so:

    foreach( $metadata as $key => $value ) {
        update_user_meta( $wcm_users[$i]->ID, $key, '' );
    }
    

    ...will probably do what you want, anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题
  • ¥15 光纤中多普勒频移公式的推导
  • ¥15 怎么制作一个人脸识别门禁系统
  • ¥20 大华dss监控平台网络关闭登不进去
  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包