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.

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

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题