• Hello,

    I’ve been facing a weird problem with the function wp_insert_post();

    It seems to do a duplicate of the created post.

    Here is the code i’m using:

    function create_post(){
        $args = [
            'post_title' => 'test',
            'post_content' => 'ok',
            'post_type' => 'post',
            'post_status' => 'draft',
            'post_author' => 1
        ];
    
       $post =  wp_insert_post($args, true, false);
       print_r($post);
    }
    create_post();

    It’s on fresh wordpress install, using last version, added directly in the functions.php file.

    Also noticed that when i comment the “create_post()” function and reload the page, it still runs one more time. A second reload and it’s not running anymore.

    I’ve been digging the problem, and it seems that it’s actually not the wp_insert_post() functions that’s called multiple times (edited that core function, printed something, and there was only one output).

    I don’t really have the time to dig more, and for now, i’ll be using wpdb directly.

    Am i the only one with that problem, or am i missing something ?

    • This topic was modified 2 years, 9 months ago by riri91.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s a bit risky to execute some WP functions directly from functions.php. WP is not completely stable when this code is executed. You generally want to call WP functions from a filter or action callback. add_action() and add_filter() are two of the few functions that are safe to execute from functions.php.

    If you want something to happen on every request, you could hook the “init” action and do something from there. You might still get two posts inserted for every request. You cannot assume any particular action or filter hook will only fire once per request. But with hooks you can do something about it. After creating a post, have your callback remove itself from the action stack. Subsequent firings of the hook will then not involve the callback any longer, until the next request anyway.

    Thread Starter riri91

    (@riri91)

    Hello bcworkz, thank you for your answer.

    Actually, i putted it in the functions.php file for debug purpose.

    I’m writing a simple script that connect to an external API and import posts from it.

    Moderator bcworkz

    (@bcworkz)

    Where will the final script actually reside? Where that is might only execute once? functions.php does appear to execute twice. I don’t know why, but it’s not that surprising to me. It’s reasonable to assume anything could execute more than once. To avoid double post insertion, you could set a constant when the insertions are finished. Wrap the insertion routine in a conditional which checks for the constant and skips insertions if set.

    In order to properly utilize WP resources, your script needs either reside in a theme or a plugin. With plugin code in particular, WP is unstable and many WP functions will not work correctly if called when the plugin is loaded. Thus placing code in an action callback is pretty much required. Less so for theme code, but still recommended.

    With code in an action callback, it can ensure one time through execution by removing itself from the call stack after the first time through. Slightly easier and more efficient IMO than setting and checking for a constant.

    That said, would you really want to get posts from the API with every request? It seems excessive, plus it’ll slow down normal requests for site content. You could set a WP cron event to periodically hit the API at regular intervals. Or only do so on demand by making a specific request, such as /wp-admin/admin-post.php?action=hit_api or similar.

    I get that your initial code was just a proof of concept exercise. That’s fine. It has exposed an issue. I’m suggesting appropriate ways forward that are most compatible with how WP does things. There are more hacky ways of doing things as well. They may be fine for your specific site, but might not work on other sites.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_insert_post duplication’ is closed to new replies.