why does the number of post abnormally be increased by wp_insert_post
-
I wanted to add a new post using hook, but I found it was always abnormally increased when I reload.
First time reloading, adding a new post.
Second time reloading, adding another 2 new posts.
Third time reloading, adding 4 new posts……The following is my code in plugin/my_plugin.php:
function example_function(){ /******************************************************* ** POST VARIABLES *******************************************************/ $postType = 'post'; // set to post or page $userID = 1; // set to user id $categoryID = '3'; // set to category id. $postStatus = 'future'; // set to future, draft, or publish $leadTitle = 'Exciting new post today: '.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 *******************************************************/ $post_id = wp_insert_post($new_post); /******************************************************* ** SIMPLE ERROR CHECKING *******************************************************/ $finaltext = ''; if($post_id){ $finaltext .= 'Yay, I made a new post.<br>'; } else{ $finaltext .= 'Something went wrong and I didn\'t insert a new post.<br>'; } echo $finaltext; echo 'post_id is:'.$post_id; } add_action('init', 'example_function');
Does anyone help me solve this question?
I just want to add a new post after every reload.
Thanks
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘why does the number of post abnormally be increased by wp_insert_post’ is closed to new replies.