Anyone encountered this issue?

I have created a custom php script which adds a new post by using wp_insert_post() but everytime I run the code it creates a 2 new post where in it should be only be one, its like it runs twice, so I tested another function using wpdb->query and its also the same its really weird. Please help I dont know what causes and how to fix this issue.

Thank you very much.

Here is a sample code i did just to test the issue and it still creates 2 identical post

   function testtest(){
      $ads_data = array(
            'post_title'    => "test",
            'post_content'  => "test",
            'post_status'   => 'publish',
            'post_type' => 'ads',
            //'post_author' => $user_id

        // Insert the post into the database
        $ads_id = wp_insert_post( $ads_data );


I have figured it out now, whats causing the issue is on my header.php it is this line of code:

<link rel="icon" type="image/png" href="<?php echo esc_url( get_theme_mod( 'favicon' ) ); ?>" />

I find it really weird, i have no idea why that line of code is causing the issue, anyway thanks everyone!

  • What is the context of your code, where does it run, what action is it hooked to?
    – Milo
    Jan 12, 2015 at 18:10
  • @Milo i just created a shortcode to run it, ive edit the code now
    – Ramje
    Jan 12, 2015 at 18:16
  • Do you have any plugins installed? Or have you tried running this code with a basic WP install? Jan 12, 2015 at 18:29
  • @czerspalace ive disabled all my plugins, and its still like that, this wasn't a problem actually before with my other sites. Maybe ill try with a basic wp install, thanks
    – Ramje
    Jan 12, 2015 at 18:32
  • 1
    your code inserts a single post when I test it, so a guess would be that more than one request is hitting the page containing the shortcode. this is obviously not a safe way to execute code, you should be taking steps to insure something runs once if you only want it to run once.
    – Milo
    Jan 12, 2015 at 18:34

1 Answer 1


I resolved my similar issue (see below code block) by modifying the path to wp-load.php. Actually I used Frankie Jaret's uri parse script which really only works on the default install but, meh, that's what I use. I'm sure you can find a function that will parse it out where ever it may reside. Code below

$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );

I don't know if this will help you out but it solved the issue for me.

Similar Issue: I realize this isn't an answer but I can't make a comment since this is my first post and I have 0 rep. But I have this exact same problem I just started to see it recently and have no favicon issues as I run the insert from a cronjob. I've been able to replicate it at least 30 consecutive times today. I will post the answer once I've figured it out. But I do know for a fact only the specific number of post arrays are being passed yet it still creates two posts, or customs in my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.