0

The ID column is auto-increment in the database, it seems. Whenever a new row is added to the wp_posts, the ID is automatically incremented.

How can I create a pre-generated ID that the wp_insert_post will use?

My code would look something like:

function generateRandomPostID( $id ) {
     return $id + generateRandomInt(); //assuming it's an int.
}
add_filter( 'insertion_of_post_filter_ID', 'generateRandonPostID' );

Here's my intent as well as my setup:

I'm trying to generate export data with the WXR Exporter that comes packed with WordPress that will later on be imported using the WXR Importer. Unfortunately, whenever I generate my export data, there is a high chance that the posts might be duplicated - assuming a customer has posts with the same IDs (high chance) already on his site, mine will not get imported.

I will have a lot of export files, all within the same category and there's a high chance for duplicated, since, for every export file, I start the export with a fresh install of WordPress. Duplicated post IDs are a clear no go.

As for the solution, I believe I can't go back to retro-actively change the post IDs because there are terms and relationships being established already and if my post ID gets changes, suddently all of the meta / relationships are erased - or perhaps wp_update_post takes care of that as well, but since I'd need to write functionality to directly change the post's ID, it doesn't really sound that nice.

The nice, easy way to do it is to simply tell WordPress that I want any post's ID that gets created to be a high int, something like 63467 when it's creating a post, so there are extremely low chances of conflict with whatever my customer will have installed already.

This can be a hack and it's no problem. This is an internal tool I'm building that's used for development only, the feature's sole purpose is to generate an unique ID for the posts. It doesn't need to respect anything, it just needs to make sure that the meta / relationships and whatever usually gets created / linked with wp_insert_post is also taken care of.

This means I don't have to write code nor for the exporter, nor for the importer, but looking at wp_insert_post, I can't exactly pinpoint where exactly should I hook. The import_id definitely looks good - it says it'll try to import it with that ID and if it already exists, just default to the increment, but I've had no success so far.

  • 1
    Could you explain why you need to do this? You run the risk of collisions if you don't just use the auto-increment value. – Jacob Peattie Dec 20 '18 at 14:44
  • @JacobPeattie I need my posts to have an unique ID from the very second they're being conceived. If I export my posts with a non-unique ID, there will be major issues when importing my posts over another site because the WXR Importer just simply skips anything with the same post ID (so if the site already has post ID 8, it just gets skipped). To add to the pain, I can't force the WXR Importer to import posts with a new ID because terms rely on this ID from the xml file itself to work. I need to give my posts a new, randomized ID at creation. – coolpasta Dec 20 '18 at 14:49
  • -- I will be careful to create the posts firsts, then the required terms around them, for all the importing site is concerned, the IDs will just look like big ints, that'll be the only weird part about it, but it'll work. Naturally, if the importing site has 50,000 posts, then the conflicts will still appear, but this is a very edge case, close to impossible. – coolpasta Dec 20 '18 at 14:56
  • Seems like the logical fix here is the importer, not generating random IDs. – Milo Dec 20 '18 at 16:08
  • @Milo It cannot be done. Any help, even a direction would help greatly. Please and thank you. This is not a bad idea for what I need precisely. I don't understand what's bad about forcing WordPress (for development purposes only) to insert with ID 4532 instead of ID 1 at first, it'd be just as if there were 4500 posts before it. I just need some unique IDs for the posts, I export then and then I'm done with the function that tricks WordPress to do this. – coolpasta Dec 20 '18 at 17:01
1

I did this and it gave me a post with the set import_id:

    $my_post = array(
        'import_id' => 1234567890,
        'post_title'    => 'Custom ID',
        'post_content'  => 'This post has a custom ID.',
        'post_status'   => 'publish',
    );
    wp_insert_post( $my_post );

In addition this will raise ID/index to the integer you set in the import_id so that subsequent posts' ids will be incremented form there (ie. 1234567891, 1234567892, etc.) - allowing you to use your large integer as a base for your posts with custom IDs.

Please give it a try and let me know how it goes.

  • This is the normal way to do it, yes, but unfortunately, I need a filter. – coolpasta Dec 21 '18 at 11:18
  • On second thought, this is exactly what I was looking for. It inserts a new post with my ID and it artificially rises the post ID count so that every post I create afterwards will use my big int as a reference point. Just re-word your answer to reflect this as a solution. For it not to just be a hack, one could also keep track of whatever is importer afterwards and delete these posts once he's done with his work so that the original int of ID is reset and WordPress will resume it's normal "counting". – coolpasta Dec 21 '18 at 12:41
  • @coolpasta I have updated my answer, please let me know if it needs any more work to be accurate. Also It might be tricky to get the system to start back at a lower ID and increment from there. I think you'd have to reset the table's AUTO_INCREMENT back to the last post ID of the lower integers. – Justin Waulters Dec 21 '18 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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