我找到了如何动态创建帖子的方法。我不知道这是否是最佳的,但它完成了工作。
我将所有必要的信息放入变量/数组中,并将它们添加到执行插入的for循环中。它会遍历整个xml文件,但如果遇到具有相同标题的帖子(我知道这可能不是最好的唯一标识符),它会停止并跳出循环,这样就不会创建多个相同的帖子。一旦中断,它会告诉用户创建了多少帖子:
$flag = true;
for($i=0; $i<24; $i++){
if(!get_page_by_title($b[$i]->title, OBJECT, post)){
//check what category it\'s in.
if(stristr($feedKeywordsArray[$i],"news")){
$category = 3;
}
elseif(stristr($feedKeywordsArray[$i], "live")){
$category =4;
}
elseif(stristr($feedKeywordsArray[$i], "insider")){
$category =5;
}
elseif(stristr($feedKeywordsArray[$i], "local")){
$category =6;
}
elseif(stristr($feedKeywordsArray[$i], "spotlight")){
$category =7;
}
else{
$category=1;
}
$my_post = array(
\'post_title\' => $b[$i]->title,
\'post_content\' => $b[$i]->description,
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_category\' => array($category),
\'tags_input\' => $feedKeywordsArray[$i]
);
// Insert the post into the database.
$post_id = wp_insert_post( $my_post );
echo "<h1> Post: \'" . $b[$i]->title . "\' added</h1>";
if($post_id>0){
update_field(\'excerpt\', (string)($b[$i]->description), $post_id);
update_field(\'vamp-thumbnail\', (string)($thumbnail[$i]), $post_id);
update_field(\'vamp-video\', str_replace("#autostart=on;", "#autostart=off;hidecompanion=on;autoadv=off;",(string)($a[$i][0])), $post_id);
}
}
else{
$flag = false;
echo "<h2> Coming Out of the for loop after creating " . $i . " successful post(s).</h2>";
break;
}
}
if($flag){
echo "Created all post";
}
?>