7

I understand that post ID's are unique, but are they reliable as persistent data ID's?

More specifically, is there a guarantee that, through a migration or another data exchange, post ID's will not change?

I understand wordpress is primarily implemented using MySQL, so ID's are governed by the AUTO_INCREMENT mechanism; however, is there any caveat on Wordpress' end that would change this behavior in the future, or potentially with another persistence implementation (such as MongoDB)?

5 Answers 5

6

The pedantic answer is NO.

While IDs are unique they can change without any change in UX as long as the change retains the consistency of the DB. And while creating a new post will generate a new unique ID, you can also create a post via code to reuse some "old" ID.

In practice they are reliable, but if reliability is very important to you then you need to attach some data to the post which will be used as the reliable identifier for your own needs.

Update: totally forgot about it because it is rarely used, and most of the time I seen it used it was used incorrectly, but wordpress has a reliable unique identifier by definition and it is the GUID

12
  • 1
    It all depends on what level of reliability you need. The uniqueness of the ID is just a byproduct of the auto increment and in no place it is defined as the field that identifies the post, and I totally forgot about it when I wrote the answer, but there is a field that by definition is a unique reliable identifier of the content and it is the guid codex.wordpress.org/Changing_The_Site_URL#Important_GUID_Note Commented Oct 8, 2014 at 13:17
  • 1
    @guidod, nothing is reliable against bad code which doesn't respect standards.ID is not even a standard. Commented Oct 9, 2014 at 20:58
  • 1
    hmmm, and of course it doesn't change on export and import and it contains URI not URL. Commented Oct 9, 2014 at 21:18
  • 2
    As for "ID is not even a standard", not sure what you mean. Having autoincrement IDs on MySQL databases is as standard (de facto, if you will, but standard anyway) as you can get. The GUID will get changed changed if you, or anyone in the future, replace old domain -> new domain in a WordPress database, which many people do (definitely much more often than manually tinkering with the IDs).
    – guidod
    Commented Oct 10, 2014 at 1:43
  • 1
    ID is just a number with no assigned meaning, unlike a Global Unique IDentifier. Just because so many people are guilty in not understanding GUID and thinking it is a URL doesn't make ID more reliable then it is, it just makes GUID less reliable. Commented Oct 10, 2014 at 3:48
9

I understand that post ID's are unique, but are they reliable as persistent data ID's?

Yes.

6
  • 2
    Might just be the answer of the month right here Commented Oct 7, 2014 at 22:11
  • 4
    The question specifically mentions "through a migration" - from what I remember the WP exporter/importer process will change post IDs to match the new system's. IE if you export posts 1-10 from Site A and import them to Site B which has 20 posts already, your new posts' IDs will be 21-30.
    – Lexi
    Commented Oct 8, 2014 at 10:19
  • Where does it mention "migration"? And, as an aside, "migration of a database" is not the same as "using the WordPress Importer". If you migrate your DB, post IDs will most certainly remain the same. Commented Oct 8, 2014 at 11:42
  • 2
    through a migration or another data exchange in the second paragraph. I would say the importer qualifies as "another data exchange".
    – Milo
    Commented Oct 8, 2014 at 17:29
  • +1 on the comment - must have not read said part. Twice. And hence an upvote to @LexR's comment too. Commented Oct 8, 2014 at 20:47
4

To recap everything that has been said, the answer is:

Yes, IDs are reliable for this, as long as you don't use the WordPress XML exporter/importer to take the posts in question into a different installation. As long as you migrate the existing database and don't install WordPress from scratch, IDs will remain the same.

PS1: Yes, as Mark Kaplun states, IDs can change or be reused if you manually change or reuse them yourself, but you already know that, and it's true for any field on any database ever.

PS2: The wp_posts GUID field should not be trusted as constant, since they depend on the original posts' URLs and can inadvertently be replaced with new URLs when the website gets migrated, and obviously the GUID would change as well when using the exporter/importer.

1
1

The answer can be Both Yes and No.

Id needs to be unique for one site. You know, if there were two id, the content can not be served. As WP use id to serve content for post and pages. WordPress loop is designed to work like this. So, you can use it for your theme or plugin development. There will be no problem with this.

But it can be a pain for you if you export content from one website to another. As the new website might generate unique id for the newly added content. In that case the id can not be a good solution.

So you can use slug instead of ids.

P.S. Slug also can be changed. But it will not automatically changed when you import content from one website to another.

Hope this helps. Sabbir

0

Post ID's are not necessarily unique. Ref. my answer on the post you linked to: https://wordpress.stackexchange.com/a/209832/71131

Within a Wordpress Multisite Network, id's are not unique. A blog post on one site can have the same id as a blog post on another site. Probably since each site has a separate database/table. (This just happened to me.) This is problematic if you in your code (in a plugin, or in a third party service communicating with the Wordpress installation) are using the Wordpress id's to refer to a specific unique blog post on the Wordpress Multisite Network.

Therefore, I recommend using $post->guid instead, which is what Wordpress has dedicated as the Global Unique IDentifier. Thanks to @Mark Kaplun for making me aware of this. NB: The guid must be in lowercaps for it to work.

@guidod mentioned the risk that "[the guid can] inadvertently be replaced with new URLs when the website gets migrated", because someone might do a find/replace on all URI's, and/or want to update the URI part of the GUID as well, even though they shouldn't ever change anything in the GUID. To mitigate this risk, you can also store the $post->ID as a backup. If your third party service accessing and referring to blog posts in your Wordpress installation cannot find a post based on the guid, then the post's ID can be attempted (but remember to contextualize it based on the appropriate blog/site, if you're doing it on a multisite installation).

1
  • NB: The guid must be in lowercaps for it to work.
    – Magne
    Commented Dec 1, 2015 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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