1

So I'm working on a plugin for wordpress, and a novice at it as well. I need to use the ID of a wordpress post, or blog article rather, as a unique reference id, or lets say in database terms, a Unique or Primary key.

Does the ID ever change on a blog?

Extra background info..:

I'm using this function to get the id of a unique post:

the_ID();

Also, the URL of a blog post on my test site has a parameter with a number (ID number?):

?p=87

But I've seen blogs where the title is included in the URL instead of the parameter p=87.

When I print out the function the_ID();, it returns the parameter number 87. Can I be certain that it doesn't change and use it on external sites for linking purposes?

3

No they don't change, but you shouldn't rely on them for external sites.

If we're talking about purely in the database on an individual WordPress site, then:

  • If it's a published/established post/page then yes, the ID will not change other than to delete it
  • If its a revision, a draft, etc the ID won't change, but it won't survive for very long either. It would be a poor piece of data to attach to

Once we move outside of that realm though, we enter the realm of data portability. If you export the data or rely on RSS, the post ID isn't available, and doesn't survive imports and exports.

So What Should I Do If I'm...

For Tables and storing references to posts in the database or Post Meta

Use Post IDs.

For detecting duplicates on other sites/programs outside your WordPress install

Use post GUIDs. These are passed around via RSS and others, and should remain the same so long as your posts URL doesn't wildly change.

To put in my code

Never hardcode post IDs in a theme, they're what we call 'magic values', and they're horrificly bad practice in programming. Not just WordPress, but any code that has these is bad code by definition. AVoid like the plague.

Use a post ID stored in an option with a GUI to set it in the dashboard area, or pull post IDs from posts in a term with a known slug.

For a custom table or custom data storage

I would advise you rely instead on custom taxonomies and post meta. Your custom tables are very likely to be both unnecessary, and not as performant as they'll not be picked up by a lot of the caching mechanisms.

If you're doing this so you can list latest blogposts on site A in the sidebar of site B

Then you should either pull them in using RSS ( perhaps a custom widget so you can style them appropriately ), or use a multisite installation.

Internally though, always refer to a post by its ID

Want to know about Terms and Categories

Same applies, use term IDs internally, use Term slugs externally.

URLs

You mentioned URLs. The reason you have ?p=86 etc is because you don't have permalinks enabled.

But if you did have permalinks enabled, the ?p=86 url would still work.

E.g. These URLs on my site both work:

Other peoples permalinks may not take the same format.

  • 1
    Thanks Tom for your in depth answer! The post not only answered my question but gave me more insight into what I should use. – Ihsan Apr 17 '14 at 4:40
2

When a post is saved, all the data is saved in the database, and a unique ID is assigned to that specific post. All information and attachments for that post is also linked to the post's unique ID. This ID will never change, this post will forever have this ID. The post keeps this ID until the post is PERMANENTLY deleted

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.