• am I using the wp_insert_post() function to create posts within a loop.

    The code works correctly, however, it duplicates the inserts.

    For example, if looping runs 1x it creates 2 posts. if looping is performed 7 times it creates 14 posts.

    It first inserts all posts, then duplicates them in the same sequence, as if the code were executing twice.

    The code is being executed in a specific one page template.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Tellyworth

    (@tellyworth)

    Inserting posts from a (presumably public) page template seems risky. Most likely the page is being fetched twice without you realising it. Or the template is loaded a second time in another context.

    Can we see your code?

    Thread Starter cassiotalle

    (@cassiotalle)

    I found that if I’m logged in doubles, if the page loads without being logged in it works perfectly. I found this information on this link https://stackoverflow.com/questions/18397400/wp-insert-post-creates-two-pages?rq=1 , the curious thing is to know why it happens.
    As you said yourself, for security purposes, this page should only be performed by the site administrator after login.
    it ‘ s my code:

    function add_post($conteudo){
        $my_post = array(
          'post_title' => $conteudo['titulo'] ,
          'post_status' => 'draft',
          'post_autor' => 1,
          'post_type' => 'corrida',
          'post_content' => '',
        );
    
        $post_id = wp_insert_post( $my_post);
        update_field( 'data', $conteudo['data'], $post_id );
        update_field( 'site', $conteudo['link'], $post_id );
    
        return $post_id;
    }
    Moderator bcworkz

    (@bcworkz)

    You should always assume WP actions might fire more than once per request. The fact this happens is a little known but long standing phenomena. I don’t know why it happens, but it has been doing so for a long time. It usually doesn’t make much difference since most callbacks can tolerate repeating the same process more than once, which is why it is infrequently observed to do so.

    The solution is to have your callback remove itself from the action stack once it has completed its operations. This way it will only run once per request as you would expect.

    Thread Starter cassiotalle

    (@cassiotalle)

    how to do this?

    Moderator bcworkz

    (@bcworkz)

    My apologies, I misspoke earlier. While what I said is accurate, it doesn’t apply to your situation. You still need code that ensures your function is only called once, something like:

    // assumes $post_data already defined and is in scope
    if ( ! defined('DO_IT_ONCE')) {
       define('DO_IT_ONCE', true );
       add_post( $post_data );
    }

    BTW, “add_post” isn’t a very good function name because it could conflict with other plugin or theme code because it’s fairly generic. It’s recommended that function names be prefixed with some distinct, unique characters such as the initials or name of the associated plugin or theme. cas_add_post() is preferable over add_post()

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘wp_insert_post save twice’ is closed to new replies.