Table of Contents
1. Disable article revision
2. Delete article revisions
3. Delete unnecessary attachments
4. Disable automatic saving
5. Rearrange discontinuous article IDs
Home CMS Tutorial WordPress A brief analysis of how to solve the problem of discontinuous article IDs in WordPress (perfect method)

A brief analysis of how to solve the problem of discontinuous article IDs in WordPress (perfect method)

Mar 15, 2023 pm 08:34 PM
php wordpress

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!

A brief analysis of how to solve the problem of discontinuous article IDs in WordPress (perfect method)

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);
Copy after login

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;
}
Copy after login

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';
Copy after login

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');" ) );
Copy after login

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' );
Copy after login

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!)

A brief analysis of how to solve the problem of discontinuous article IDs in WordPress (perfect method)

Recommended learning: "

WordPress Tutorial"

The above is the detailed content of A brief analysis of how to solve the problem of discontinuous article IDs in WordPress (perfect method). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

How to write a header of a wordpress How to write a header of a wordpress Apr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

What to do if there is an error in wordpress What to do if there is an error in wordpress Apr 20, 2025 am 11:57 AM

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

See all articles