我已经制作了这个简单的代码来向WordPress DB添加新的POST(在LOOP之外):
include_once './wp-config.php';
include_once './wp-load.php';
include_once './wp-includes/wp-db.php';
$upname = 'TestName';
$updescription = 'Description';
// Create post object
$my_post = array();
$my_post['post_title'] = $upname;
$my_post['post_content'] = $updescription;
$my_post['post_status'] = 'draft';
$my_post['post_author'] = 1;
if (current_user_can( 'manage_options' )) {
// Insert the post into the database
wp_insert_post( $my_post );
echo '<br /><b>The Advertisement is saved as Draft!</b>';
} else {
echo '<br /><b>You are NOT logged in, login to continue!</b>';
}
它100%有效但现在我想添加2个自定义字段:“phone_num”和“birth_date”
如何在此代码中添加自定义字段?
答案 0 :(得分:1)
我找到了一个解决方案,这对我有用: - )
在我的代码中,我更新/替换了
// 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);
答案 1 :(得分:0)
你应该使用它,它会对你有所帮助。
我使用过这个,它运行正常。
// 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);
感谢。