我有一个连接到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);
感谢您的帮助。
答案 0 :(得分:14)
您可以使用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
答案 1 :(得分:6)
很惊讶在wp-includes / post.php中没有提到post_exists
功能。见entry on wpseek。在手抄本中没有条目。最简单的方法就像get_page_by_title
一样,但返回一个帖子id(如果没有找到则返回0)而不是对象(或null)。
$post_id = post_exists( $my_title );
if (!$post_id) {
// code here
}
答案 2 :(得分:4)
对于迟到的回复感到抱歉。我在评论中使用了机器人所说的内容,这解决了我的问题。感谢
$post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_title like '$title_from_soap'");
if($post_if < 1){
//code here
}
答案 3 :(得分:0)
采样器:
if( !get_page_by_path('mypageslug',OBJECT,'post') ){
//your codes
}