我有一個連接到肥皂服務器的wordpress網站。問題是每次運行腳本時,「wp_insert_post」都會再次使用相同的結果。我想檢查現有的post_title是否與$ title的值匹配,如果它們匹配,請防止wp_insert_post再次使用相同的值。如何通過在運行「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);
感謝您的任何幫助。
你應該試試這個代碼。 require(dirname(__ FILE__)。'/wp-load.php'); global $ wpdb; echo $ count = $ wpdb-> get_var(「select $ COUNT(*)from $ wpdb-> posts'post_title' like'$ title'」); – Robot