Fanly

Fanly

一个摸爬打滚于 IT 互联网的追梦人!

How to obtain non-sequential post IDs in WordPress

Most programmers have an extreme pursuit of their own code, and I am no exception. As a WordPress enthusiast, the issue 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 an article, and then retrieve these deleted article IDs when writing an article.

ddb5ae295f86b9eb98499cc7bfb069b3_WordPress-Post-ID

Although it seems that the problem has been solved, this feature can only be applied to new content created in the future. For old websites like ours at Leixue Network that have been operating for many years, we still cannot obtain the IDs of previously deleted articles. So today, I will solve this problem. The solution is very simple, just use the following code to retrieve the non-sequential IDs in the wp_posts table of the WordPress database.

// 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 retrieves all 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 it is not, then 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 non-sequential IDs.

Although it is possible to find the unused IDs and make WordPress automatically use these IDs through development, it cannot be guaranteed that these IDs will remain unused. Manually setting the IDs in WordPress may cause data inconsistency and other issues. Therefore, when developing, please use it according to the actual situation and needs of your website. You can also make some judgments when using these new IDs to avoid problems.

Note: The purpose of the line require('./wp-load.php'); 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 Leixue Blog, and any form of reproduction is prohibited.

Article link: https://zhangzifan.com/wordpress-get-skip-post-id.html

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.