• Resolved nobody123

    (@nobody123)


    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

    • This topic was modified 2 years, 5 months ago by nobody123.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Hello,

    I would try to play it on another hook like plugins_loaded to check if it changes something or not.

    Thread Starter nobody123

    (@nobody123)

    Thank for your answer, but this problem still wasn’t solve after I modified my code.
    add_action('init', 'example_function');->add_action('plugins_loaded', 'example_function');

    First time reloading, adding a new post.
    Second time reloading, adding another 3 new posts.
    Third time reloading, adding 4 new posts……

    • This reply was modified 2 years, 5 months ago by nobody123.

    I think you must add some checks at start of the function.
    There’s background function who are running and go by init hook.

    Example: wp_cron
    But I don’t know how to return if not a real visitor .

    Thread Starter nobody123

    (@nobody123)

    I understand the reason about adding too much posts using init.
    But I don’t know why do I add some checks at start of the function.
    Dose it help us solve problem?
    why?
    and how to do specifically?
    do you have try it?
    Or do you have another suggestion for adding new post using hook or any way?

    When I say check, I mean add a IF condition at the beginning of your function like

    if ( is_wp_cron ) {
    return;
    }
    

    That way, only real visitor are creating a post, not the WPCron task (for example)

    is_wp_cron is an example, I do not think it exists

    Moderator bcworkz

    (@bcworkz)

    You cannot assume any hook will only fire once per request. If your callback is such that it’s a problem if it’s called more than once, have it remove itself from the call stack after the first pass through so it does not get called again until the next request. Use the remove_action() function to remove action callbacks. Callbacks can remove themselves.

    Hi @nobody123 Can you please check with wp action?

    I tried your code with this action : add_action(‘wp’, ‘example_function’); in theme and its working. Only 1 post is added on each page reload.

    Let me know this will work for you or not.

    Thread Starter nobody123

    (@nobody123)

    @sebastienserre Is your mean letting the code init not callback again by if condition?
    If it is what I say, how does write the condition of if?

    @bcworkz Thanks for your suggestion.
    But it becomes to produce three posts after reload every times…

    @weboccults Thank you very much!!
    It work!!

    Thanks for everybody answer!!!

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.