Where exactly should I hook in order to change the post's ID before insertion in the database?

admin2025-06-05  3

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.

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.

Share Improve this question edited Dec 20, 2018 at 20:52 coolpasta asked Dec 20, 2018 at 14:28 coolpastacoolpasta 9691 gold badge9 silver badges24 bronze badges 8
  • 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 Commented Dec 20, 2018 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 Commented Dec 20, 2018 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 Commented Dec 20, 2018 at 14:56
  • Seems like the logical fix here is the importer, not generating random IDs. – Milo Commented Dec 20, 2018 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 Commented Dec 20, 2018 at 17:01
 |  Show 3 more comments

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749076842a316158.html

最新回复(0)