Wordpress 如何通过在运行“wp_insert_post"之前检查帖子标题是否存在来防止重复帖子? [英] Wordpress how to prevent duplicate post by checking if post title exist before running "wp_insert_post"?

查看:20
本文介绍了Wordpress 如何通过在运行“wp_insert_post"之前检查帖子标题是否存在来防止重复帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到soap服务器的wordpress站点.问题是每次我运行脚本时 wp_insert_post 都会再次使用相同的结果.

I have a wordpress site that connects to a soap server. The problem is every time I run the script the wp_insert_post is using the same result again.

我想检查现有的 post_title 是否与 $title 中的值匹配,然后如果它们匹配,则防止 wp_insert_post 使用相同的值再次.

I would like to check if existing post_title matches the value from $title then if they match, prevent wp_insert_post from using the same value again.

代码如下:

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);

感谢您的帮助.

推荐答案

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

Sorry for the late response. I used what Robot says in the comment and this solved my problem. Thanks

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

这篇关于Wordpress 如何通过在运行“wp_insert_post"之前检查帖子标题是否存在来防止重复帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆