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

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

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

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
This article is reproduced at:露兜即刻. If there is any infringement, please contact admin@php.cn delete
Is WordPress suitable for e-commerce?Is WordPress suitable for e-commerce?May 13, 2025 am 12:05 AM

Yes, WordPress is very suitable for e-commerce. 1) With the WooCommerce plugin, WordPress can quickly become a fully functional online store. 2) Pay attention to performance optimization and security, and regular updates and use of caches and security plug-ins are the key. 3) WordPress provides a wealth of customization options to improve user experience and significantly optimize SEO.

How to add your WordPress site in Yandex Webmaster ToolsHow to add your WordPress site in Yandex Webmaster ToolsMay 12, 2025 pm 09:06 PM

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

How to fix HTTP image upload errors in WordPress (simple)How to fix HTTP image upload errors in WordPress (simple)May 12, 2025 pm 09:03 PM

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader

How to fix the issue where adding media buttons don't work in WordPressHow to fix the issue where adding media buttons don't work in WordPressMay 12, 2025 pm 09:00 PM

Recently, one of our readers reported that the Add Media button on their WordPress site suddenly stopped working. This classic editor problem does not show any errors or warnings, which makes the user unaware why their "Add Media" button does not work. In this article, we will show you how to easily fix the Add Media button in WordPress that doesn't work. What causes WordPress "Add Media" button to stop working? If you are still using the old classic WordPress editor, the Add Media button allows you to insert images, videos, and more into your blog post.

How to set, get and delete WordPress cookies (like a professional)How to set, get and delete WordPress cookies (like a professional)May 12, 2025 pm 08:57 PM

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

How to Fix WordPress 429 Too Many Request ErrorsHow to Fix WordPress 429 Too Many Request ErrorsMay 12, 2025 pm 08:54 PM

Do you see the "429 too many requests" error on your WordPress website? This error message means that the user is sending too many HTTP requests to the server of your website. This error can be very frustrating because it is difficult to find out what causes the error. In this article, we will show you how to easily fix the "WordPress429TooManyRequests" error. What causes too many requests for WordPress429? The most common cause of the "429TooManyRequests" error is that the user, bot, or script attempts to go to the website

How scalable is WordPress as a CMS for large websites?How scalable is WordPress as a CMS for large websites?May 12, 2025 am 12:08 AM

WordPresscanhandlelargewebsiteswithcarefulplanningandoptimization.1)Usecachingtoreduceserverload.2)Optimizeyourdatabaseregularly.3)ImplementaCDNtodistributecontent.4)Vetpluginsandthemestoavoidconflicts.5)ConsidermanagedWordPresshostingforenhancedperf

How customizable is WordPress, really?How customizable is WordPress, really?May 11, 2025 am 12:11 AM

WordPress is very customized, providing a wide range of flexibility and customizability. 1) Through the theme and plug-in ecosystem, 2) use RESTAPI for front-end development, 3) In-depth code level modifications, users can achieve a highly personalized experience. However, customization requires mastering technologies such as PHP, JavaScript, CSS, etc., and pay attention to performance optimization and plug-in selection to avoid potential problems.

See all articles

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 Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft