【问题标题】:Wordpress how to prevent duplicate post by checking if post title exist before running "wp_insert_post"?Wordpress如何通过在运行“wp_insert_post”之前检查帖子标题是否存在来防止重复帖子?
【发布时间】:2011-11-30 22:11:47
【问题描述】:

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

感谢您的帮助。

【问题讨论】:

  • 你应该试试这个代码。要求(目录名(文件)。'/wp-load.php');全球 $wpdb; echo $count = $wpdb->get_var("select COUNT(*) from $wpdb->posts where post_title like '$title'");

标签: wordpress soap wsdl custom-wordpress-pages posts


【解决方案1】:

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

【讨论】:

  • 我喜欢这种利用内置函数来获得最佳效果的方法。我相信第一行应该是:“if (!get_page_by_title($title, OBJECT, 'skarabeepublication') ) :"
  • OBJECT 不应该有撇号,否则此方法可以完美运行,并且从 WordPress 4.7.3 开始仍然有效。
  • 它还会返回草稿和垃圾帖。
【解决方案2】:

很惊讶在 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
}

【讨论】:

【解决方案3】:

抱歉回复晚了。我使用了机器人在评论中所说的内容,这解决了我的问题。谢谢

$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
}

【讨论】:

    【解决方案4】:

    采样器:

    if( !get_page_by_path('mypageslug',OBJECT,'post') ){
      //your codes
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 2011-02-12
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      相关资源
      最近更新 更多