WordPress-Importer icon indicating copy to clipboard operation
WordPress-Importer copied to clipboard

Do not auto increment IDs

Open themenow opened this issue 8 years ago • 5 comments

Terms in import file:

    <wp:category><wp:term_id>2</wp:term_id><wp:category_nicename><![CDATA[beauty]]></wp:category_nicename><wp:category_parent><![CDATA[]]></wp:category_parent><wp:cat_name><![CDATA[Beauty]]></wp:cat_name></wp:category>
    <wp:category><wp:term_id>4</wp:term_id><wp:category_nicename><![CDATA[casual]]></wp:category_nicename><wp:category_parent><![CDATA[]]></wp:category_parent><wp:cat_name><![CDATA[Casual]]></wp:cat_name></wp:category>
    <wp:category><wp:term_id>3</wp:term_id><wp:category_nicename><![CDATA[life]]></wp:category_nicename><wp:category_parent><![CDATA[]]></wp:category_parent><wp:cat_name><![CDATA[Life]]></wp:cat_name></wp:category>
    <wp:category><wp:term_id>32</wp:term_id><wp:category_nicename><![CDATA[misc]]></wp:category_nicename><wp:category_parent><![CDATA[]]></wp:category_parent><wp:cat_name><![CDATA[Misc]]></wp:cat_name></wp:category>
    <wp:category><wp:term_id>33</wp:term_id><wp:category_nicename><![CDATA[team]]></wp:category_nicename><wp:category_parent><![CDATA[misc]]></wp:category_parent><wp:cat_name><![CDATA[Team]]></wp:cat_name></wp:category>
    <wp:category><wp:term_id>1</wp:term_id><wp:category_nicename><![CDATA[uncategorized]]></wp:category_nicename><wp:category_parent><![CDATA[]]></wp:category_parent><wp:cat_name><![CDATA[Uncategorized]]></wp:cat_name></wp:category>
    <wp:tag><wp:term_id>10</wp:term_id><wp:tag_slug><![CDATA[computer]]></wp:tag_slug><wp:tag_name><![CDATA[Computer]]></wp:tag_name></wp:tag>
    <wp:tag><wp:term_id>8</wp:term_id><wp:tag_slug><![CDATA[freedom]]></wp:tag_slug><wp:tag_name><![CDATA[Freedom]]></wp:tag_name></wp:tag>
    <wp:tag><wp:term_id>23</wp:term_id><wp:tag_slug><![CDATA[interactive]]></wp:tag_slug><wp:tag_name><![CDATA[Interactive]]></wp:tag_name></wp:tag>
    <wp:tag><wp:term_id>40</wp:term_id><wp:tag_slug><![CDATA[photo]]></wp:tag_slug><wp:tag_name><![CDATA[photo]]></wp:tag_name></wp:tag>
    <wp:tag><wp:term_id>9</wp:term_id><wp:tag_slug><![CDATA[sofa]]></wp:tag_slug><wp:tag_name><![CDATA[Sofa]]></wp:tag_name></wp:tag>
    <wp:tag><wp:term_id>6</wp:term_id><wp:tag_slug><![CDATA[web-design]]></wp:tag_slug><wp:tag_name><![CDATA[Web Design]]></wp:tag_name></wp:tag>
    <wp:tag><wp:term_id>5</wp:term_id><wp:tag_slug><![CDATA[wordpress]]></wp:tag_slug><wp:tag_name><![CDATA[WordPress]]></wp:tag_name></wp:tag>
    <wp:term><wp:term_id><![CDATA[28]]></wp:term_id><wp:term_taxonomy><![CDATA[tn_portfolio_category]]></wp:term_taxonomy><wp:term_slug><![CDATA[portfolio]]></wp:term_slug><wp:term_parent><![CDATA[]]></wp:term_parent><wp:term_name><![CDATA[Portfolio]]></wp:term_name></wp:term>
    <wp:term><wp:term_id><![CDATA[26]]></wp:term_id><wp:term_taxonomy><![CDATA[tn_portfolio_category]]></wp:term_taxonomy><wp:term_slug><![CDATA[psd]]></wp:term_slug><wp:term_parent><![CDATA[portfolio]]></wp:term_parent><wp:term_name><![CDATA[PSD]]></wp:term_name></wp:term>
    <wp:term><wp:term_id><![CDATA[24]]></wp:term_id><wp:term_taxonomy><![CDATA[tn_portfolio_category]]></wp:term_taxonomy><wp:term_slug><![CDATA[beauty]]></wp:term_slug><wp:term_parent><![CDATA[portfolio]]></wp:term_parent><wp:term_name><![CDATA[Beauty]]></wp:term_name></wp:term>
    <wp:term><wp:term_id><![CDATA[37]]></wp:term_id><wp:term_taxonomy><![CDATA[tn_portfolio_category]]></wp:term_taxonomy><wp:term_slug><![CDATA[fashion]]></wp:term_slug><wp:term_parent><![CDATA[portfolio]]></wp:term_parent><wp:term_name><![CDATA[Fashion]]></wp:term_name></wp:term>
    <wp:term><wp:term_id><![CDATA[35]]></wp:term_id><wp:term_taxonomy><![CDATA[tn_portfolio_category]]></wp:term_taxonomy><wp:term_slug><![CDATA[luxury]]></wp:term_slug><wp:term_parent><![CDATA[portfolio]]></wp:term_parent><wp:term_name><![CDATA[Luxury]]></wp:term_name></wp:term>
    <wp:term><wp:term_id><![CDATA[25]]></wp:term_id><wp:term_taxonomy><![CDATA[tn_portfolio_category]]></wp:term_taxonomy><wp:term_slug><![CDATA[mockups]]></wp:term_slug><wp:term_parent><![CDATA[portfolio]]></wp:term_parent><wp:term_name><![CDATA[Mockups]]></wp:term_name></wp:term>
    <wp:term><wp:term_id><![CDATA[12]]></wp:term_id><wp:term_taxonomy><![CDATA[tn_portfolio_category]]></wp:term_taxonomy><wp:term_slug><![CDATA[nonprofit]]></wp:term_slug><wp:term_parent><![CDATA[portfolio]]></wp:term_parent><wp:term_name><![CDATA[Nonprofit]]></wp:term_name></wp:term>
    <wp:term><wp:term_id><![CDATA[14]]></wp:term_id><wp:term_taxonomy><![CDATA[tn_portfolio_category]]></wp:term_taxonomy><wp:term_slug><![CDATA[photography]]></wp:term_slug><wp:term_parent><![CDATA[portfolio]]></wp:term_parent><wp:term_name><![CDATA[Photography]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>11</wp:term_id><wp:term_taxonomy>nav_menu</wp:term_taxonomy><wp:term_slug><![CDATA[main-nav]]></wp:term_slug><wp:term_name><![CDATA[Main Nav]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>34</wp:term_id><wp:term_taxonomy>nav_menu</wp:term_taxonomy><wp:term_slug><![CDATA[mobile-menu]]></wp:term_slug><wp:term_name><![CDATA[Mobile Menu]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>16</wp:term_id><wp:term_taxonomy>nav_menu</wp:term_taxonomy><wp:term_slug><![CDATA[portfolio-nav]]></wp:term_slug><wp:term_name><![CDATA[Portfolio Nav]]></wp:term_name></wp:term>
    <wp:term><wp:term_id>18</wp:term_id><wp:term_taxonomy>nav_menu</wp:term_taxonomy><wp:term_slug><![CDATA[user-login]]></wp:term_slug><wp:term_name><![CDATA[User login]]></wp:term_name></wp:term>

After import, the wp_terms table of target site (clean before import):

term_id_got_reset

IDs are reset in the term's order in import file

themenow avatar Apr 20 '16 13:04 themenow

The ID fields for all posts and terms are auto-increment so whenever a post is inserted the ID is created automatically. It's possible to use the importer to add new posts to a site rather than overwrite everything, so we can't count on the term id being the same.

I've been bitten by this a lot when shortcodes or widgets or themes hard-code the ID. Issue #41 is an example.

I wonder if the importer could first determine whether or not the posts or terms database table is empty, and if it is, then copy the ID from the old site to the new site?

cbergen avatar Apr 20 '16 20:04 cbergen

The problem is WordPress itself hard-code the ID too, for example, when you assign a menu to a location in Appearance > Menus or Appearance > Customize, the data is stored in theme mod 'nav_menu_locations' like this

[nav_menu_location] = array(
    'location_slug' => 'menu_id',
    'location_slug' => 'menu_id'
)

Then you import everything to another site, but menus won't show up, their IDs are changed thus WordPress couldn't find them.

themenow avatar Apr 21 '16 04:04 themenow

It's possible to use the importer to add new posts to a site rather than overwrite everything, so we can't count on the term id being the same.

I'll add a note that I've been bitten also by this when using hierarchical post types no longer matching parent/child posts.

ntwb avatar Apr 21 '16 11:04 ntwb

It's possible to use the importer to add new posts to a site rather than overwrite everything, so we can't count on the term id being the same.

That's correct.

I wonder if the importer could first determine whether or not the posts or terms database table is empty, and if it is, then copy the ID from the old site to the new site?

How often does this happen? Would the end user know that the result is different if the database is clean or not? On a fresh WordPress install, you already have a post and a category anyway, so it won't ever start at index 1.

Also, I don't think that the current importer does this, so this seems like an unexpected change.

swissspidy avatar May 23 '16 16:05 swissspidy

I'll add a note that I've been bitten also by this when using hierarchical post types no longer matching parent/child posts.

This should be handled already.

It seems like the actual issue at hand here is that menu item IDs aren't updated?

rmccue avatar Jun 13 '16 09:06 rmccue