GET_USER_META值存储在奇怪的字符中

时间:2021-03-22 作者:Kaleshe Alleyne-Vassel

我正在尝试使用访问存储在用户配置文件中的IDadd_user_meta 以及get_user_meta 函数返回ID,但当我打印出这些值时,会得到这个奇怪的值。我可以看到ID在里面,但我不太明白如何对它进行编码,只显示值,而不显示所有其他内容。[school_id] => Array ( [0] => a:1:{i:0;i:88;} )

How the data is being added

/*
* Create new custom post type post on new user registration
*/
add_action( \'user_register\', \'add_user_to_school_cpt\', 10, 1 );
function add_user_to_school_cpt( $user_id )
{
   // Get user info
   $user_info = get_userdata( $user_id );

   // Create a new post
   $school_post = array(
       \'post_title\'   => get_field(\'name\', \'user_\' . $user_id), 
       \'post_content\' => $user_info->description,
       \'post_type\'    => \'school\',
       \'post_status\'  => \'publish\',
       \'post_author\'  => $user_id,
   );

   // Insert the post into the database
   $school_id = wp_insert_post( $school_post );

   // Add the school ID as a meta data
   add_user_meta( $user_id, \'school_id\', [$school_id] );

   // Add school custom fields
   add_post_meta( $school_id, \'email\', $user_info->user_email );
   add_post_meta( $school_id, \'school_telephone\', get_field(\'telephone\', \'user_\' . $user_id) );
   add_post_meta( $school_id, \'school_city\', get_field(\'city\', \'user_\' . $user_id) );
   add_post_meta( $school_id, \'school_postcode\', get_field(\'postcode\', \'user_\' . $user_id) );
   add_post_meta( $school_id, \'school_address\', get_field(\'address\', \'user_\' . $user_id) );
} ```

1 个回复
SO网友:Nour Edin Al-Habal

错误在于这一行:

   // Add the school ID as a meta data
   add_user_meta( $user_id, \'school_id\', [$school_id] );
存储用户元时无需使用数组括号,请将其更改为:

   // Add the school ID as a meta data
   add_user_meta( $user_id, \'school_id\', $school_id );

相关推荐

Meta-value query

有点长篇大论,但试图解释清楚:)我正在钻研我的新主题的代码,不熟悉这种类型的数据存储/拉取-有人能帮我理解这里发生了什么吗,请:我试图创建一个页面,只输出附有“优惠券”的帖子优惠券\'存储为帖子的meta\\u密钥。它是meta_value 存储为如下数组(使用打印输出):a: 7:{s:9:“突出显示”;s:14:“测试突出显示”;s:5:“标题”;s:12:“食品五折”;s:11:“描述”;s:16:“测试描述”;s:4:“代码”;s:7:“1524521”;s:11:“弹出图像”;s:4:“4548