WordPress如何通过在运行“wp_insert_post”之前检查帖子标题是否存在来防止重复帖子?

发布于 2024-12-08 07:13:00 字数 1504 浏览 2 评论 0原文

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

谢谢您的帮助。

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.

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.

Here's the code:

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

Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月静好 2024-12-15 07:13:00

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

法典信息此处

You can use get_page_by_title() as it supports custom post types now.

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;

Codex information here

站稳脚跟 2024-12-15 07:13:00

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

$post_id = post_exists( $my_title );
if (!$post_id) {
    // code here
}

Surprised not to see mention of post_exists function in wp-includes/post.php. See entry on wpseek. There is no entry in the codex. At it's simplest it works like get_page_by_title but returns a post id (or 0 if not found) instead of the object (or null).

$post_id = post_exists( $my_title );
if (!$post_id) {
    // code here
}
放飞的风筝 2024-12-15 07:13:00

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

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

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
}
随梦而飞# 2024-12-15 07:13:00

采样器:

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

sampler:

if( !get_page_by_path('mypageslug',OBJECT,'post') ){
  //your codes
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文