Reputation: 3324
I have added a post in my wordpress site using php including the code below. I deleted the post from my wordpress admin and tried to add it again using the same code but post_exists returns true instead of false.
What is wrong? Any help appreciated.
<?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;
?>
The result of $finaltext is : - Post title already exists i dont need to add it again
Upvotes: 0
Views: 489
Reputation: 3324
I have found the solution. After you trash the post the file is not immediately deleted. You need to go to the trash select the post you want to permantly delete and select delete permantly from the drop down menu and hit apply. This nearly drive me crazy!
Upvotes: 1