How to solve the problem of discontinuous article IDs in WordPress? The following article will share with you how WordPress can perfectly solve the problem of discontinuous article IDs. I hope it will be helpful to you!
There are many WordPress blogs that use article IDs in their fixed links. However, WordPress has caused the problem of discontinuous article IDs due to various reasons. There are often bloggers in the forum. Ask questions to resolve the issue, but rarely get a satisfactory answer from them.
Today I will tell you how to solve this problem perfectly. To say it is perfect may be a bit too much. Versions after WordPress 3.0 will have an automatic draft, and each article published will occupy two IDs. No. There is currently no way to disable it. Please continue reading to see how to solve the problem of discontinuous article IDs to a certain extent.
Of course, if you do not use post ID in your fixed link, then the post ID is transparent to you and does not matter. Even if the post ID is used in the permalink, if you don't particularly care, even the discontinuity is irrelevant, and this article will not make much sense to you.
1. Disable article revision
The so-called article revision means that every time you modify an article, it will automatically save the article version before modification for you, professional The term is called version control, which ensures that the previous content can be restored in the event of mistaken modification. This is very helpful in maintaining Wiki documents, but as our small blog, it does not seem to be of much use, and this revised version It occupies an ID in the database, which is also one of the problems that causes article IDs to be discontinuous. To disable article revisions, you can add:
define('WP_POST_REVISIONS', false);
to the wp-config.php file. You can also add the following PHP code to functions.php of the current theme:
// 禁用修订版本,2015年3月5日更新 add_filter( 'wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2 ); function specs_wp_revisions_to_keep( $num, $post ) { if ( 'post_type' == $post->post_type ) $num = 0; return $num; }
2. Delete article revisions
After disabling article revisions, the previously created article revisions are still saved in the database. These are actually gone. How useful it is, and it occupies the ID, we can delete it. As for how to delete, you can execute the following SQL statement in phpmyadmin (it will affect the top article, use with caution! And make a backup):
DELETE a,b,c FROM wp_posts a LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id) LEFT JOIN wp_postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision';
3. Delete unnecessary attachments
I believe that many bloggers will upload/insert some attachments at the same time when publishing articles, such as images, videos, music, etc. These attachments can be seen in the WordPress management background - media library, and different media correspond to Different articles. But you should note that these media also occupy the article ID, and they are stored in the same database table wp_posts as the article. If you particularly pursue that the IDs of the articles must be perfectly continuous, please do not upload/insert these media when publishing the article, and please delete the previously uploaded media in the WordPress management background - media library (note that this operation is not just deletion) records, and the files you uploaded will be deleted), please use FTP to re-upload these files if necessary.
4. Disable automatic saving
The advantage of automatic saving is that when you edit an article, the system will automatically save the edited article for you every short period of time. This prevents the webpage from suddenly closing, resulting in the thousands of words written previously being lost. The disadvantage is that each article will have an automatically saved record, which also occupies an article ID, which is one of the reasons why the article IDs are not consecutive. If you do not need this function, you can add the following code to the functions.php of the current theme:
// 禁用自动保存,所以编辑长文章前请注意手动保存。 add_action( 'admin_print_scripts', create_function( '$a', "wp_deregister_script('autosave');" ) );
5. Rearrange discontinuous article IDs
Method 1: You can add the following PHP code to functions.php of the current theme , so if you just post an article without posting a page, adding a menu, or uploading media, basically the subsequent article IDs will be continuous, and the IDs of previously published articles will not be changed, and SEO will not be affected:
// WordPress 3.8测试有效 function keep_id_continuous(){ global $wpdb; // 删掉自动草稿和修订版 $wpdb->query("DELETE FROM `$wpdb->posts` WHERE `post_status` = 'auto-draft' OR `post_type` = 'revision'"); // 自增值小于现有最大ID,MySQL会自动设置正确的自增值 $wpdb->query("ALTER TABLE `$wpdb->posts` AUTO_INCREMENT = 1"); } add_filter( 'load-post-new.php', 'keep_id_continuous' ); add_filter( 'load-media-new.php', 'keep_id_continuous' ); add_filter( 'load-nav-menus.php', 'keep_id_continuous' );
If you use functions such as pinned articles, menus, and parent-child pages, the following method will not work:
Method 2: Complete the above steps to basically guarantee that articles will be published in the future. The IDs will be continuous, but the IDs of previously published articles are still in chaos. They need to be rearranged to ensure that the IDs are continuous. I wrote a PHP script. You can download it from the following URL. After downloading, open it with a text editor. Modify the database information according to the instructions at the beginning. Then upload the PHP file to your blog space. Run it and see OK. , you can go to phpmyadmin to check whether the IDs in the wp_posts table are consecutive. Again, please back up your database before starting. (If you use post id as a fixed link, it may change the URLs of all articles, which will affect search engine inclusion; if you use a plug-in to create a new database table, such as a voting plug-in, etc., problems will also occur; if A page with a parent-child relationship is created in your blog. If you run the following script, this relationship will be lost. Please use with caution!)