gpt4 book ai didi

wordpress - fatal error : Uncaught Error: Cannot use object of type WP_Error as array

转载 作者:行者123 更新时间:2023-12-02 19:54:41 25 4
gpt4 key购买 nike

我正在尝试从 wordpress 表中获取用户数据。当我将数据放入数组时,它显示了一个 fatal error 。

这是我的 php 代码:

$getuser_data = $wpdb->get_results($user_query);
$userdata = array();
foreach($getuser_data as $data)
{

$userfeed['username'] = $data->user_login;
$userfeed['name'] = $data->user_email;
$userfeed['email'] = $data->display_name;

$userfeed['user_id'] = $data->ID;
$userfeed['profile_pic'] = get_wp_user_avatar_src($data->ID);
$usercount = count($getuser_data);


}
$userdata[] = $userfeed;

它在最后一行 $userdata[] 中显示 fatal error Cannot use object of type WP_Error as array。任何帮助,将不胜感激。提前致谢。

最佳答案

$user_query 可能有问题,因此 $wpdb->get_results($user_query) 返回了 WP_Error 对象而不是查询结果。将代码包装在以下 block 中并检查错误是什么。

if( !is_wp_error( $getuser_data ) ) {
$userdata = array();
foreach($getuser_data as $data)
{

$userfeed['username'] = $data->user_login;
$userfeed['name'] = $data->user_email;
$userfeed['email'] = $data->display_name;

$userfeed['user_id'] = $data->ID;
$userfeed['profile_pic'] = get_wp_user_avatar_src($data->ID);
$usercount = count($getuser_data);

$userdata[] = $userfeed;
}

} else {
echo $getuser_data->get_error_message();
}

关于wordpress - fatal error : Uncaught Error: Cannot use object of type WP_Error as array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57308032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com