7

我有一个连接到肥皂服务器的 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);

感谢您的任何帮助。

4

4 回答 4

16

您现在可以使用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;

法典信息在这里

于 2011-12-03T13:23:20.680 回答
7

惊讶地没有看到post_existswp-includes/post.php 中提到的功能。请参阅wpseek 上的条目。法典中没有条目。在最简单的情况下,它的工作方式类似于get_page_by_title但返回一个帖子 ID(如果未找到,则返回 0)而不是对象(或 null)。

$post_id = post_exists( $my_title );
if (!$post_id) {
    // code here
}
于 2014-08-11T01:57:45.027 回答
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
}
于 2012-10-07T07:11:12.600 回答
0

采样器:

if( !get_page_by_path('mypageslug',OBJECT,'post') ){
  //your codes
}
于 2015-04-13T08:40:38.847 回答