我有一个连接到soap 服务器的wordpress 站点。问题是每次我运行脚本时 wp_insert_post
再次使用相同的结果。
我想检查是否存在 post_title
匹配来自 $title
的值然后,如果它们匹配,则阻止 wp_insert_post
再次使用相同的值。
这是代码:
try {
$client = new SoapClient($wsdl, array('login' => $username, 'password' => $password));
} catch(Exception $e) {
die('Couldn\'t establish connection to weblink service.');
}
$publications = $client->GetPublicationSummaries();
foreach ($publications->GetPublicationSummariesResult->PublicationSummaries->PublicationSummary as $publication_summary) {
// get the complete publication from the webservice
$publication = $client->getPublication(array('PublicationId' => $publication_summary->ID))->GetPublicationResult->Publication;
// get all properties and put them in an array
$properties = array();
foreach ($publication->Property as $attribute => $value) {
$properties[$attribute] = $value;
}
// Assemble basic title from properties
$title = $properties['Address']->Street . ' ' . $properties['Address']->HouseNumber . $properties['Address']->HouseNumberExtension . ', ' . $properties['Address']->City->_;
}
$my_post = array(
'post_title'=>$title,
'post_content'=>'my contents',
'post_status'=>'draft',
'post_type'=>'skarabeepublication',
'post_author'=>1,
);
wp_insert_post($my_post);
感谢您的任何帮助。
最佳答案
您可以使用 get_page_by_title()
因为它现在支持自定义帖子类型。
if (!get_page_by_title($title, OBJECT, 'skarabeepublication')) :
$my_post = array(
'post_title'=>$title,
'post_content'=>'my contents',
'post_status'=>'draft',
'post_type'=>'skarabeepublication',
'post_author'=>1,
);
wp_insert_post($my_post);
endif;
法典信息 here
关于Wordpress 如何通过在运行 "wp_insert_post"之前检查帖子标题是否存在来防止重复帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7652294/