3

When trying to create a new blog post, Wordpress is showing a Database error:

 WordPress database error Duplicate entry '0' for key 'PRIMARY' for query INSERT INTO `wp_posts` (`post_author`,`post_date`,`post_date_gmt`,`post_content`,`post_content_filtered`,`post_title`,`post_excerpt`,`post_status`,`post_type`,`comment_status`,`ping_status`,`post_password`,`post_name`,`to_ping`,`pinged`,`post_modified`,`post_modified_gmt`,`post_parent`,`menu_order`,`post_mime_type`,`guid`) VALUES (5,'2014-10-13 11:19:21','0000-00-00 00:00:00','','','Auto Draft','','auto-draft','post','open','open','','','','','2014-10-13 11:19:21','0000-00-00 00:00:00',0,0,'','') made by get_default_post_to_edit, wp_insert_post PHP Warning: Creating default object from empty value in C:\..\..\..\httpdocs\wp-admin\includes\post.php on line 567

I am able to edit past posts but the issue appears when trying to create a new post.

Any suggestions on a work around or fix to stop new posts from defaulting to the 0 as the Primary Key?

Wordpress V4.0
PHP 5.4

0

2 Answers 2

5

This is only my best guess. I can't see a definitive answer with the information provided.


The primary key for wp_posts should be set to ID. The primary key is giving the error. Since ID is not defined in the query, it is likely that the AUTO_INCREMENT value has been set to 0, or is no longer incrementing.

I would look into your database and find the AUTO_INCREMENT value. I bet it's set to 0 for some reason.

Take a backup of your database before making any changes to the database.

If the AUTO_INCREMENT value is set to 0, you want to set it to a value that hasn't yet been reached. Go to wp_posts and find the highest value ID and set it to one more than that (or a few more than that, just to be safe).

For example, if the highest value ID in wp_posts is 1000:

ALTER TABLE wp_posts AUTO_INCREMENT = 1001;

If this is not the issue, ensure that the PRIMARY_KEY for the table is set to ID.

4
  • Wow - thank you for your help RadGH. Much appreciated! Commented Oct 13, 2014 at 21:06
  • Tried, but that did not fix the issue.
    – Riz
    Commented Dec 13, 2019 at 6:57
  • @Riz It only fixes the auto increment number being too low for a table. If you are trying to import an SQL file or insert a row manually while specifying post ID, you can't do that if IDs are already taken. You'll run into problems if the post also has postmeta because the ID must be the same for those rows. So you can't just change it either. Might be worth posting a new question. Commented Dec 13, 2019 at 20:06
  • 1
    Interestingly for me the problem was that AUTO_INCREMENT wasn't set for the ID. Make sure sure A_I is set for ID.
    – phrogg
    Commented Nov 17, 2023 at 14:10
0

I wrote an update for this.

I uses the build in WP Core schema to ensure all WP Core tables are there (even in the future when 6.0 or higher is released ). It removes corrupt rows and re-adds keys an primary keys. The script (and more explanation) can be seen over here: https://wpindexfixer.tools.managedwphosting.nl/

No need to guess the auto-increment value too.

It is a free script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.