请参阅教程,其中解释了如何从CSV读取数据,以及如何创建和更新现有帖子。它还解释了如何更新acf字段。
http://www.pearlbells.co.uk/insert-udpate-wordpress-post-programmatically/
粘贴用于创建和更新帖子的代码。
function postExists($postCSVContent,$parentId , $mydb) {
$mydb->get_results($mydb->prepare(\'SELECT ID FROM `wp_posts` WHERE ID = %d\',$postCSVContent[0]));
if( $mydb->num_rows > 0) {
echo \'Post \'.$postCSVContent[0].\' exist\'.PHP_EOL;
$template_file = get_post_meta( $postCSVContent[0], \'_wp_page_template\', true );
echo \'Template File : \'.$template_file.PHP_EOL;
$updatePost = array(
\'ID\' => $postCSVContent[0],
\'post_title\' => $postCSVContent[\'1\'],
\'post_content\' => $postCSVContent[\'2\'],
\'post_type\' => \'doors\',
\'post_status\' => \'publish\',
\'post_author\' => 1
);
wp_update_post( $updatePost );
update_post_meta( $postCSVContent[0], \'_wp_page_template\', $template_file );
updateAcf( $postCSVContent , $postCSVContent[0] );
}
else
{
echo \'Post \'.$postCSVContent[0].\' is not existing\'.PHP_EOL;
echo \'Post parent \'.$parentId.PHP_EOL;
$newIds = wp_insert_post( array(
\'post_title\' => $postCSVContent[\'1\'],
\'post_content\' => $postCSVContent[\'2\'],
\'post_type\' => \'doors\',
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_parent\' => $parentId
));
updateAcf( $postCSVContent , $newIds );
return $newIds;
}