0

I am running an fgetcsv query to import a bunch of data from a CSV into WordPress.

I am wondering how I can start an auto increment from a certain number, for example, from 1000 onwards.

$import1="INSERT into wp_postmeta (meta_id,post_id,meta_key,meta_value) values(',',',','first_name','$data[1]')";

This is an example of the code. the meta_id should use a normal auto increment, but the post_id I want to start from a certain number.

How would I accomplish this?

Zach Nicodemous
  • 9,097
  • 9
  • 45
  • 68

2 Answers2

3

To set the starting value for an auto increment field, you can use alter table

ALTER TABLE wp_postmeta AUTO_INCREMENT = 1000;

As far as I know, it is not possible to have two auto incrementing fields on the same table in mysql, so you have to do it progamatically yourself (eg with a trigger)

Lepidosteus
  • 11,779
  • 4
  • 39
  • 51
  • Where would I insert that? Also, wouldn't that affect the auto-increment offset for both the meta_id and post_id columns? – Zach Nicodemous Jan 22 '13 at 20:43
  • 1
    You should have a look at insert triggers in mysql documentation. For exemple, make the tigger set the value of meta_id for new rows to MAX(meta_id) + 1; and keep the regular auto increment field on post_id – Lepidosteus Jan 22 '13 at 20:45
  • Two auto incrementing fields in a single table is a code smell (design smell?) that indicates you should really have two separate tables. – AgentConundrum Jan 22 '13 at 20:54
  • Thats how its laid out in the WordPress table AgentC, but I guess they use another method to increment the second column – Zach Nicodemous Jan 22 '13 at 20:55
  • @ZachNicodemous I'm no expert on WordPress, but from a [quick Google search](https://codex.wordpress.org/images/9/9e/WP3.0-ERD.png), it seems like `wp_postdata.post_id` acts like a foreign key to `wp_posts.id`. It should be set based on that, rather than independently as its own auto_increment field. – AgentConundrum Jan 22 '13 at 21:03
1

To change the AUTO_INCREMENT value on the table:

ALTER TABLE wp_postmeta AUTO_INCREMENT = 1000;
ste
  • 286
  • 1
  • 2
  • 9
  • I need to change the auto_increment value for a column within the table, because only post_id should be affected. – Zach Nicodemous Jan 22 '13 at 20:43
  • 1
    There will only be one column defined with AUTO_INCREMENT - it'll change which ever column is defined – ste Jan 22 '13 at 20:45