• I’m having a problem with my post IDs getting duplicated and overwritten. I have an external events database that I am tapping into with wp_insert_posts and have been importing IDs along with post content. However, if I made a post previously with that happens to have the same ID as one of the events, the former gets overwritten and replaced with the new event. Since these imported events have to have this very specific ID that follows a very specific scheme (they all start with 3), I want all my other posts to have an ID that is very different so that there is no chance of being overwritten.

    Is there a way to tell wordpress to always generate IDs that start with a certain number or follow any certain defined scheme?

Viewing 1 replies (of 1 total)
  • The ID field of the database is a auto-increment, so it will go up +1 for every post created unless ID is supplied on insertion.

    You can alter your database scheme to increment by a different number but basically starting with a 3 will not work from a database standpoint.

    Something you could do is add a action to hook into the save_post or wp_insert_post.

    WP Insert post will have a flag if the post is being created or updated, so that may be the best route.
    WP Action: save_post | wp_insert_post

    In the function you can check the post_id and set it however you would like as long as the ID isn’t already in use.

    • This reply was modified 9 years, 7 months ago by James. Reason: Add in wp_insert_post option
Viewing 1 replies (of 1 total)

The topic ‘How to dynamically set post IDs by post type’ is closed to new replies.