What are the best practices for using post_id? Can I use this safely in my templates to call specific posts, or will these post_id's ever change?

The reason I ask is that randomly today the post_id's changed on my site, and I'm wondering what could have caused it. It didn't mess up any of the permalinks (thankfully!) but I did have to go switch some custom queries which were calling content specifically by the post_id (specifically in some custom_post_types I've created)

So... should I use a different query to get a specific post? The query I'm using looks like this.

<?php query_posts('post_type=home_feature&p=36')?>

Just earlier today, post_id for this post was 139 and my query was:

<?php query_posts('post_type=home_feature&p=139')?>

Obviously, when the post_id changed, my query (and subsequently the custom home feature) broke. I'm just not sure what could have caused this change.. and if that means I shouldn't be using the post_id in custom queries.. Thoughts?

  • I don't think a post id can ever change. Its the primary key id from the posts table. So something must have been changed in your db if your post_ids got changed. That said.. its still not a good idea to use the hardcoded post_ids. – Hameedullah Khan Jun 30 '11 at 5:10
  • how would you reference specific pages in the hard coding (if not using post_ids)? – timshutes Jul 1 '11 at 14:03
  • If its a single post, then you can use the meta key. If you need to reference multiple posts, you can use the categories. – Hameedullah Khan Jul 1 '11 at 16:31
  • Can you give me an example of this query? that would be a preferable query in this case, since the post ID seems to be changing... – timshutes Oct 2 '11 at 4:15

Post ID's do not change. The reason behind this is the ID column in MYSQL is the primary key for the table. It is auto incremented when a new row is added. It is improbable that a post ID would change programatically unless someone is doing something rogue in the database.

  • It actually just happened again on this site. There is no other evidence of problems in the DB, but the custom query broke again for no apparent reason. I checked, and sure enough - the about page went from p=2 to p=36. I triple checked with my client, and no one deleted / resaved, or anything at all that should cause this change in the DB. any thoughts at all? – timshutes Oct 2 '11 at 4:13
  • could a plugin cause this possibly? It seems to happen when I add a sidebar... I'm at a loss – timshutes Oct 2 '11 at 4:15
  • How is the custom query being made? If it's using query_posts that might be the issue. Can you show me in your question what the query looks like? – Brian Fegter Oct 2 '11 at 20:35
  • I'm marking this as the answer. Indeed - they are not supposed to change. We figured out this was happening when new sidebars were created.. something fishy in the functions.php code. Sorry for the lengthy wait - I thought I already marked this answer. Appreciate the help – timshutes Feb 6 '12 at 22:27

Why not just do <?php query_posts( 'p=36' )?> There shouldn't be a need to specify the post type as post ID's are supposed to be unique.

Or perhaps you could target the post by Title and get the ID from that:

$page = get_page_by_title('Title of Post','post');
query_posts( 'p=' . $page->ID );

I would recommend you a different way to go, for sample, instead of the id, just go with the page's name, or create a meta data (custom field) with a check mark and call it.

For sure you will be safe that it breaks in the future!

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.