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.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵