---
title: "Renumbering the ID field of the wp_posts table"
description: "A few weeks ago one of our followers raised a database problem in a Google+ group. In his post José Costa Ros raised a serious problem that had come up for one of his clients, specifically the online shop TuBabyShop,"
url: https://artprojectgroup.com/renumbering-the-id-field-of-the-wp_posts-table
date: 2016-05-31
modified: 2026-09-19
author: "Art Project Group"
image: https://artprojectgroup.com/wp-content/uploads/2023/04/woocommerce-blog.webp
categories: ["Tutorials"]
tags: ["MySQL", "PHP", "SQL", "WooCommerce"]
type: post
lang: en-US
---

# Renumbering the ID field of the wp_posts table

A few weeks ago one of our followers raised a database problem in a Google+ group.

In his post José Costa Ros raised a serious problem that had come up for one of his clients, specifically the online shop [TuBabyShop](https://tubabyshop.es/), with the *ID* field of the *wp_posts* table, which had reached a total length of 13 digits, which made it impossible for him to take payment for orders through Redsys.

The problem is caused by the strict rules of Redsys when it comes to processing orders, since it makes the use of 4 digits and 8 characters essential, and as the *ID* field had a code 13 digits long it was materially impossible to complete the orders in WooCommerce, since the code received, the first 4 digits of the 13 of the order number, made it impossible to know which order the payment referred to and so they were always left waiting for payment.

The best solution that can be given to a problem like the one our friend José raised is to renumber the *ID* field of the *wp_posts* table of the database, but how?

### Solution

To do so, the first thing we did was look at what had happened in the *wp_posts* table and we discovered that the problem had arisen almost a year earlier when, all of a sudden, it jumped from code 1302 to a completely wild number 13 digits long. The problem most likely arose as a result of some badly executed SQL query or some kind of one-off failure in the MySQL server, although we did not dig much further either since we found no more related problems.

Once the problem had been located we proceeded to create a small PHP script with some SQL queries that we placed in the WordPress root directory and that would automate the task of renumbering the *ID* field from number 1303 onwards, but not only in the *wp_posts* table, but in all the other tables of the database that are related to it. Specifically the PHP code we used was this:

Copy to clipboardSyntax Highlighterif ( file_exists( "./wp-load.php" ) ) {
require_once( "./wp-load.php" );
} else {
exit( '404 Not Found

# Not Found

Please ensure that database.php is in the root directory' );
}

$counter = 1303;
$query = $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}posts` where ID > 1302" );

foreach ( $query as $row ) {
$ID = $row->ID;
//wp_posts
$new_query = $wpdb->query( "UPDATE `{$wpdb->prefix}posts` SET ID = $counter WHERE ID = $ID" );
$new_query = $wpdb->query( "UPDATE `{$wpdb->prefix}posts` SET post_parent = $counter WHERE post_parent = $ID" );
//wp_postmeta
$new_query = $wpdb->query( "UPDATE `{$wpdb->prefix}postmeta` SET post_id = $counter WHERE post_id = $ID" );
//wp_comments
$new_query = $wpdb->query( "UPDATE `{$wpdb->prefix}comments` SET comment_post_ID = $counter WHERE comment_post_ID = $ID" );
//wp_term_relationships
$new_query = $wpdb->query( "UPDATE `{$wpdb->prefix}term_relationships` SET object_id = $counter WHERE object_id = $ID" );
//wp_options
$new_query = $wpdb->query( "UPDATE `{$wpdb->prefix}options` SET `option_value` = replace(option_value, '$ID', '$counter' WHERE option_value LIKE '%$ID%'" );
//wp_woocommerce_order_items
$new_query = $wpdb->query( "UPDATE `{$wpdb->prefix}woocommerce_order_items` SET `order_id` = $counter WHERE order_id = $ID" );
$counter++;
}
$counter--;
$new_query = $wpdb->query( "ALTER TABLE `{$wpdb->prefix}posts` AUTO_INCREMENT = $counter;" );

echo "Process finished in " . DB_NAME;
Thanks to it we were able to reorder the *ID* field of the *wp_posts* table and of all the other tables related to this same field. In addition we modified the table’s auto-increment value to prevent those 13-digit codes from continuing to appear.

The truth is that right now this situation is no longer a problem if you use the [**WooCommerce Sermepa payment gateway**](https://wordpress.org/plugins/woocommerce-sermepa-payment-gateway/) plugin and its [new feature](https://wordpress.org/support/topic/order-id/?replies=6), requested by us ourselves. Another day we will publish a couple of functions for getting round this same problem using the new `wc_myredsys_merchant_order_encode` and `wc_myredsys_merchant_order_decode` filters.

We hope you find this small PHP script useful and that it gets you out of more than one tight spot.
