Crazy Question – Updating Post ID

First off, I have no idea WHY you would want to do this. IDs are unique for a reason. If you change the ID, I can only see it creating more problems than it solves. Can you explain why you want to do this? I’m sure the community could give you an alternate solution. But to answer the question, here goes:

No. There is no function that does that. You would need to run some code when the user saves or updates a post. It should be possible though. You could use this action hook in your theme’s functions.php file:

add_action( 'post_updated', 'my_function_name_here', 10, 3 );
function my_function_name_here($post_ID, $post_after, $post_before) {
    global $wpdb;

    // this is the custom query
    $wpdb->query($wpdb->prepare("UPDATE wp_posts SET ID=[your new ID] WHERE ID = $post_ID"));
}

Then inside that function, you can create a custom database query that will change the ID.

Note: This will fail if the new ID is already in the table, so you may want to check for the new ID’s existence first. Or solve the problem in a way that does not attempt to change IDs

I have to stress that this is not a good way to achieve whatever you are trying to achieve. Hope I helped.

Update: The OP posted this as the actual issue: “The problem I have is the devs for a property management theme decided to order the properties by their ID over say, publish date. A client of mine has rental properties that periodically are active and inactive. When we reactivate a property we would like it to be the first listings but because its ordered by ID this can’t happen unless we re-enter the property. Right now we are just cloning the property and deleting the original since this gives the cloned property a new ID.”

In this case, find the query being used to get all the posts. Instead of using ID as order_by, use these parameters: ‘post_status’ => ‘publish’, ‘orderby’ => ‘modified’, ‘order’ => ‘DESC’

This will change the query to display the most recently modified post first.

tech