I'm using WP import/export function. In exported XML file term_id is 4, but it becomes random number after import which is breaking my widgets structure. Is there any way to force or keep old term_id when importing to new website?
1 Answer
Off the top of my head, you'd have to either:
(a) bypass the import functionality in WP and do it direct to the database, so the term IDs stay the same (i.e. export/import from phpMySQL).
or
(b) write a "run once" data migration function to update your term IDs once they're imported, using a reference table.
or
(c) change your plugin/widget to detect the term slug or name instead of the ID.
I believe the last of those would be "best practice", as it ensures your plugin works regardless of the data in the back end. Using the visible/accessible parts of the data is always better, even if it's harder, longer and/or stupidly difficult with WP's available functions!
-
The reason this happens in my case is because a term with ID=9 exists for a custom post type but when i install my theme , woocommerce adds some term entries with an ID=9 and later when i import the demo data this ID is no longer for my custom post type and gets added with another ID. I think your (c) option is the best to follow.– stergoszJul 16, 2017 at 10:10
-
Did a quick search. You'll need to find your widget check for ID=9 and use get_term_by to get the ID first: codex.wordpress.org/Function_Reference/get_term_by– SpikeJul 16, 2017 at 10:49