will post_id ever change? Can I safely use post_id for custom queries?

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)

Read More

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?

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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 );
    
  3. 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!