I have to add the custom Post id for the custom post type. can anyone help me? Check screen short for more details. http://prntscr.com/mo3eyu
-
Change it how? The id is a serialized number auto generated by WordPress, which it uses to reference the post data in the database. In my opinion it's not really worth messing with – tshimkus Feb 21 '19 at 10:59
-
Thanks For the comment, I know but there is some API problem so I want to change – Harsh Khare Feb 21 '19 at 12:01
-
Thie accepted answer does not answer the question. Here is a better discussion: https://stackoverflow.com/questions/6558286/insert-post-id-with-wp-insert-post – John Dee Sep 06 '19 at 17:50
3 Answers
This is a work around that works.
Background info: We maintain a few almost identical web pages with different branding. The code repo is shared across these. The developers hard coded page IDs into the PHP in the past. I was asked to add new pages. After adding new pages on each website, I noticed the IDs were all randomly generated. This was breaking PHP code's logic. So, I needed to edit these IDs just like OP.
The work around:
In WP web admin page, there's an export / import functionality under tools menu. The new pages should be created in one site, then exported / imported onto other websites. This way, page IDs don't get created randomly. It copies the IDs from the exported page. So you can have same IDs on all pages.
PS:
Not sure why the past developers decided to hard-code page IDs in the code. This shouldn't have been the case to start with. When you don't have much time to refactor everything, stack overflow always comes to help.

- 1,482
- 3
- 17
- 26
IDs determine post record storage in database, changing them directly would be highly prone to breaking things. For example extensions often store IDs as a way to refer to specific post and do something with it.
The simple way to change to some ID would be to just create a new post and copy data over (through admin or with code either).

- 2,335
- 16
- 26
-
2Why is this answer accepted? This doesn't answer the question at all. – John Dee Sep 03 '19 at 20:52
-
-
4Yeah, well that's not very useful. There are lots of use cases for this. I am migrating a site with 10k posts and I need to preserve the post IDs. There are lots of reasons. – John Dee Sep 06 '19 at 17:51
-
@JohnDee bit late to the party but please see https://stackoverflow.com/a/64814253/3298119 for a work around – Baz Guvenkaya Nov 13 '20 at 01:42
Post ID of a post also reflect in tables other than Post table, like post comments table. Changing post ID will disturb the relationship of post table with other tables, so it is not advisable.

- 17
- 2