Wordpress 如何在运行前检查帖子标题是否存在以防止重复帖子;可湿性粉剂“插入”后;?
我有一个连接到soap服务器的wordpress站点。问题是每次我运行脚本时,“wp_insert_post”都再次使用相同的结果。我想检查现有的post_title是否与$title中的值匹配,如果匹配,请防止wp_insert_post再次使用相同的值 代码如下:Wordpress 如何在运行前检查帖子标题是否存在以防止重复帖子;可湿性粉剂“插入”后;?,wordpress,soap,wsdl,Wordpress,Soap,Wsdl,我有一个连接到soap服务器的wordpress站点。问题是每次我运行脚本时,“wp_insert_post”都再次使用相同的结果。我想检查现有的post_title是否与$title中的值匹配,如果匹配,请防止wp_insert_post再次使用相同的值 代码如下: try { $client = new SoapClient($wsdl, array('login' => $username, 'password' => $password)); } catch(
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\u page\u by\u 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;
食品法典信息很抱歉回复太晚。我用机器人在评论中说的话解决了我的问题。谢谢
$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
}
$post\u if=$wpdb->get\u var(“从$wpdb中选择count(post\u title)->post WHERE post\u title,如“$title\u FROM\u soap”);
如果($post_if<1){
//代码在这里
}
惊讶地发现wp includes/post.php中没有提到post\u
函数。看见法典中没有条目。最简单的情况是,它的工作原理类似于get\u page\u by\u title
,但返回一个post id(如果找不到,则返回0),而不是对象(或null)
取样器:
if( !get_page_by_path('mypageslug',OBJECT,'post') ){
//your codes
}
你应该试试这个代码。require(dirname(FILE)。'/wp load.php');全球$wpdb;echo$count=$wpdb->get_var(“从$wpdb->posts中选择count(*),其中post_title
类似于“$title”);我喜欢这种利用内置函数获得最佳效果的方法。我相信第一行应该是:“如果(!get_page_by_title($title,OBJECT,'skarabeepublication'):”OBJECT
不应该有撇号,但是这个方法工作得很好,从WordPress 4.7.3开始仍然有效。它还将返回草稿和垃圾帖子。WordPress开发者代码参考中现在有一个。
if( !get_page_by_path('mypageslug',OBJECT,'post') ){
//your codes
}