wordpress中的post_exists函数返回1而不是0

时间:2018-10-21 15:29:25

标签: php wordpress

我在使用PHP的wordpress网站上添加了一个帖子,其中包括以下代码。我从wordpress管理员中删除了该帖子,并尝试使用相同的代码再次添加该帖子,但是post_exists返回true而不是false。

怎么了?任何帮助表示赞赏。

<?php

require_once( '../chinesegreekoffers/wp-load.php' );
require_once( ABSPATH . 'wp-admin/includes/post.php' );

/*******************************************************
     ** POST VARIABLES
     *******************************************************/

     $postType = 'post'; // set to post or page
     $userID = 1; // set to user id
     $categoryID = '21'; // set to category id.
     $postStatus = 'publish';  // set to future, draft, or publish

     $leadTitle = 'Adding titlenew : '.date("n/d/Y");

     $leadContent = '<h1>Vacations</h1><p>Vacations are the best thing in this life.</p>';
     $leadContent .= ' <!--more--> <p>Expensive they are, but they are totally worth it.</p>';


     /*******************************************************
      ** TIME VARIABLES / CALCULATIONS
      *******************************************************/
      // VARIABLES
      $timeStamp = $minuteCounter = 0;  // set all timers to 0;
      $iCounter = 1; // number use to multiply by minute increment;
      $minuteIncrement = 1; // increment which to increase each post time for future schedule
      $adjustClockMinutes = 0; // add 1 hour or 60 minutes - daylight savings

      // CALCULATIONS
      $minuteCounter = $iCounter * $minuteIncrement; // setting how far out in time to post if future.
      $minuteCounter = $minuteCounter + $adjustClockMinutes; // adjusting for server timezone

      $timeStamp = date('Y-m-d H:i:s', strtotime("+$minuteCounter min")); // format needed for WordPress

        /*******************************************************
         ** WordPress Array and Variables for posting
         *******************************************************/

         $new_post = array(
         'post_title' => $leadTitle,
         'post_content' => $leadContent,
         'post_status' => $postStatus,
         'post_date' => $timeStamp,
         'post_author' => $userID,
         'post_type' => $postType,
         'post_category' => array($categoryID)
     );

     /*******************************************************
 ** WordPress Post Function
 *******************************************************/

 $finaltext = '';
 $post_id = post_exists( $leadTitle );
 if (!$post_id) {
        // code here

 $finaltext .= ' - Post title does not exists i am going to add it<br>';

 $post_id = wp_insert_post($new_post);

 /*******************************************************
 ** SIMPLE ERROR CHECKING
 *******************************************************/

 if($post_id){

 $finaltext .= 'Yay, I made a new post.<br>';
echo "finaltext success: ".$finaltext."<br><br>";
 } else{

 $finaltext .= 'Something went wrong and I didn\'t insert a new post.<br>';
echo "finaltext error: ".$finaltext."<br><br>";
 }

 }
 else {

  $finaltext .= ' - Post title already exists i dont need to add it again<br>';
 }



 echo $finaltext;

?>

$ finaltext的结果是:-帖子标题已经存在,我不需要再次添加

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。在您删除帖子后,该文件不会立即删除。您需要进入垃圾箱,选择要永久删除的帖子,然后从下拉菜单中选择永久删除,然后点击应用。这差点让我发疯!