Php WordPress-如何添加/更新post_meta(自定义字段)?

Php WordPress-如何添加/更新post_meta(自定义字段)?,php,wordpress,Php,Wordpress,我编写了这个简单的代码来向WordPress DB(外部循环)添加一个新帖子: include_once./wp config.php'; 包括_once'/wp load.php'; include_once'/wp includes/wp db.php'; $upname='TestName'; $updescription='Description'; //创建post对象 $my_post=array(); $my_post['post_title']=$upname; $my_post

我编写了这个简单的代码来向WordPress DB(外部循环)添加一个新帖子:

include_once./wp config.php';
包括_once'/wp load.php';
include_once'/wp includes/wp db.php';
$upname='TestName';
$updescription='Description';
//创建post对象
$my_post=array();
$my_post['post_title']=$upname;
$my_post['post_content']=$updescription;
$my_post['post_status]='draft';
$my_post['post_author']=1;
如果(当前用户可以(“管理选项”)){
//将帖子插入数据库
wp_insert_post($my_post);
echo“
广告另存为草稿!”; }否则{ echo'
您尚未登录,请登录以继续!'; }
它100%工作,但现在我想添加两个自定义字段:“电话号码”“出生日期”


如何在此代码中添加自定义字段?

我找到了一个解决方案,这对我很有用:-)

在我的代码中,我更新/替换了:

// Insert the post into the database
wp_insert_post( $my_post );
// Insert the post into the database
$post_id =  wp_insert_post( $my_post );
update_post_meta($post_id,'phone_num',$upphone);
update_post_meta($post_id,'birth_date',$upbirthdate);
使用此选项:

// Insert the post into the database
wp_insert_post( $my_post );
// Insert the post into the database
$post_id =  wp_insert_post( $my_post );
update_post_meta($post_id,'phone_num',$upphone);
update_post_meta($post_id,'birth_date',$upbirthdate);

你应该使用这个,它会帮助你更多

我用过这个,效果很好

// Insert the post into the database
$post_id =  wp_insert_post( $my_post );
add_post_meta($post_id,'phone_num',$upphone); or 
    update_post_meta($post_id,'phone_num',$upphone);
add_post_meta($post_id,'birth_date',$upbirthdate); or 
     update_post_meta($post_id,'birth_date',$upbirthdate);
谢谢。

请检查