Most programmers have an extreme pursuit of their own code, and I am no exception. As a WordPress enthusiast, the problem of non-sequential WordPress article IDs has always been on my mind. In fact, I had already solved half of the problem two years ago. The principle is to record the IDs of deleted articles when deleting articles, and then retrieve these deleted article IDs when writing articles.
Although it seems that the problem has been solved, this feature can only be applied to future new content operations. For old websites like ours at Tearsnow, which have been operating for many years, it is still impossible to obtain the IDs of previously deleted articles. So today, I will solve this problem. The processing method is very simple, just use the following code to obtain the non-sequential IDs in the wp_posts table of the WordPress database and query the skipped IDs.
// Get non-sequential post IDs in WordPress by Fanly https://zhangzifan.com/wordpress-get-skip-post-id.html
//require('./wp-load.php'); // If running in a different directory, remember to load wp-load.php
global $wpdb;
// Get the minimum and maximum post IDs
$min_max_ids = $wpdb->get_row("SELECT MIN(ID) AS min_id, MAX(ID) as max_id FROM {$wpdb->prefix}posts", ARRAY_A);
// Get all used post IDs
$used_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->prefix}posts");
$missing_ids = array();
// Traverse all IDs from the minimum to the maximum and find the unused IDs
for ($i = $min_max_ids['min_id']; $i <= $min_max_ids['max_id']; $i++) {
if (!in_array($i, $used_ids)) {
$missing_ids[] = $i;
}
}
// Print all unused post IDs
print_r($missing_ids);
This code first gets the minimum and maximum post IDs in the wp_posts table, and then gets all the used post IDs. Then it traverses all IDs from the minimum to the maximum and checks if each ID is in the list of used IDs. If not, it adds this ID to the $missing_ids array. This code may take some time to run, especially when there are a large number of articles and a large number of non-sequential IDs.
Although it is possible to find the unused IDs and let WordPress automatically use these IDs through development, it cannot be guaranteed that these IDs will remain unused. Manually setting the ID of WordPress may cause data inconsistency and other issues. Therefore, when developing, please use it according to the actual situation and needs of your website. At that time, you can definitely make some judgments when using these new IDs to avoid problems.
Note: require('./wp-load.php'); The purpose of this line of code is to include the main configuration file of WordPress (wp-load.php), so that your script can use WordPress functions and classes. However, the actual file path may vary depending on the location of your script and the installation location of WordPress. './wp-load.php' assumes that your script and wp-load.php file are in the same directory. If your script is in a different directory, you may need to use a different path. For example, if your script is in a subdirectory, you may need to use require('../wp-load.php'); to search for the file in the parent directory.
For more information about WordPress optimization and questions, you can join the QQ group: 255308000
Unless otherwise stated, all articles are original articles from Tearsnow Blog, and any form of reprint is prohibited.
Article link: https://zhangzifan.com/wordpress-get-skip-post-id.html