• Resolved Justin.Reinhart

    (@justinreinhart)


    I’m using an external PHP script to call wp_insert_post and it is returning an ID number without actually posting to the database.

    // Post and keep ID number
    $returnId = wp_insert_post ( $my_post );

    What appears to be happening is that I’m being returned recycled ID numbers for posts that were moved into the trash. I’m actually up to post 159 (at the time of this writing) but wp_insert_post is returning IDs lower than 159. When I look in MySQL to see what’s going on, I can see old posts that are in the trash with IDs that I’m being given. I can still see the original posts that were created earlier in the day programmatically and then moved to trash.

    Everything was working fine today until I moved some posts to trash and then the ID recycling began. I’m thinking ideally that WordPress should NEVER RECYCLE POST IDs and that his is possibly a critical bug with the product.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Well.. I seriously doubt the default functionality of wp_insert_post() would return an existing ID. The ID column is set to auto-increment… so any newly created table entry would be given a new auto-incremented ID (meaning it would never be “recycled”).

    It’s hard to say for sure without seeing your external PHP script; but it sounds like you may be adding a post ID in your wp_insert_post() array (from your external PHP script).

    Here is an excerpt from the WordPress Codex Page on wp_insert_post():

    IMPORTANT: Setting a value for $post[‘ID’] WILL NOT create a post with that ID number. Setting this value will cause the function to update the post with that ID number with the other values specified in $post. In short, to insert a new post, $post[‘ID’] must be blank or not set at all.

    So, perhaps somewhere in your external PHP script it is pulling an ID from an existing entry before running the wp_insert_post() function.

    Thread Starter Justin.Reinhart

    (@justinreinhart)

    HAHAHAHA. Well. Thanks for your input. Actually it wasn’t that because I was printing $my_post before calling the line and ID definitely wasn’t set.

    Turns out the relative include of “../../wp-load.php” was snagging another wordpress installation and inserting posts in the wrong wordpress installation. The numbers were lining up in a spooky way. I think I must have tweaked the location of my files at some point and the relative include took hold of another installation without an error. Looks like I have some cleaning up to do.

    Thanks for your time.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Bug: wp_insert_post returns ID number without posting’ is closed to new replies.