Skip Navigation

[Resolved] Import relational data

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

This thread is resolved. Here is a description of the problem and solution.

Problem:
Parent child relationship lost when importing posts using CSV file.

Solution:
You can use WP ALL IMPORT plugin to resolve this issue, as explained in this post:
https://toolset.com/forums/topic/import-relational-data/page/2/#post-534483

WP ALL IMPORT: https://wordpress.org/plugins/wp-all-import/

This support ticket is created 7 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 12 replies, has 2 voices.

Last updated by katjaL 7 years, 5 months ago.

Assisted by: Noman.

Author
Posts
#533296

Hi,
I try to import relational data with the help of this old thread
https://toolset.com/forums/topic/help-importing-relational-data/

I have the following structure:

myparentpost
olduid

mychildpost
olduid
parentsolduid

So I have created in Toolset the post types and the fields and the data is imported ok. Now I should make this php script work, but no luck so far. This is what I have:

<?php
require "wp-config.php";
$childs = get_posts('post_type=mychildpost’);
foreach ($childs as $child) {
$old_id = get_post_meta($child->ID, ‘wpcf-olduid’, true);
$parents = get_posts('post_type=myparentpost&meta_key=wpcf-olduid&meta_value=' . $old_id);
update_post_meta($child->ID, '_wpcf_belongs_myparentpost_id', $parents[0]->ID);
}
?>

What is wrong? Thank you!

Regards,
Katja

#533404

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Katja,

Thank you for contacting Toolset Support. As you have mentioned that custom post types and custom fields data is imported fine, then what exactly is not working? Please provide more details on this so we can help you out.

1. What you expected to see?
2. What you actually see right now?

Looking forward to help you, Thank you

#533410

Hi Noman,
Thank you for replying. The problem is that the id's are not accurate/compatible: the old data has the old system's id's of the parent records. And now that I have imported the parent records, their id is different (of course) and I need to establish the relation based on the new id's. So I need to either
1. modify the import cv's id's to new id's (this is a huge work since I have so much related data)
2. make the php script work, to update the id's to the new ones

(The problem and the solution I'm trying to implement is described here in the link I provided:
https://toolset.com/forums/topic/help-importing-relational-data/)

Please let me know if I'm still unclear.

#533467

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for providing more details. Ok so the problem is that child posts lost the parent post's relationship or parent post ID, got it. I am going to setup posts, reproduce and test the given code at my end and will update you accordingly.

The linked thread is quite old and its possible that the code is outdated or needs some changes to make it work.

#533711

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi ,

1. Please create custom field in parent post type to preserve parent post ID in it. So that we can save old post IDs in that custom field.
2. Then import parent post type.
3. Then import child post type and run previous code.
4. If still found any issue, then you can send us temporary login info to look into your site.

I have enabled private message box, your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

Thank you

#533739

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

Thank you for providing login info. I have looked into your site and found that IDs are not correct (see attached screenshot).

Can I have access to your site from where you have exported these posts with parent child relationship?

I have enabled private message box, your next answer will be private which means only you and I have access to it.

Thank you

#533969

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Katja,

I have been debugging this, tried few things and I have executed below code by adding it into functions.php file and then removed it:

$childs = get_posts('post_type=tuoteluokka');
foreach ($childs as $child) {
	$old_id = get_post_meta($child->ID, 'wpcf-tuoteryhma', true);
	$parents = get_posts('post_type=tuoteryhma&meta_key=wpcf-uid&meta_value=' . $old_id);
	update_post_meta($child->ID, '_wpcf_belongs_tuoteryhma_id', $parents[0]->ID);
}

I think now posts relations have been updated, can you please check and let me know. The example posts I have checked are:
hidden link
hidden link
hidden link

Thank you

#534090

Hi Noman, and thank you. There is something still wrong. Only part of the records have "found" their parents. For example if you check tuoteluokka "Massiivilankut" there is no parent even though it's there and the old uid's are as they should be.
I can't find any logic in this, what's ok and what's not. Can you?

#534244

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Katja,

That’s quite strange because based on the code all related posts should be updated.

Can you please provide me access to your old site from where you have exported these posts? I need to look at your old setup to see how the posts were related and investigate further, so its quite required at this stage.

I have enabled private message box, your next answer will be private which means only you and I have access to it.

Thank you for your patience and cooperation.

#534377

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I basically would like to see what was the original IDs, the existing relationship, etc. Basically we do not officially support importing relational data as it could vary from site to site. The custom field method is kind of a workaround and the old code was offered few years back by a supporter, I could not find any other tickets where the same script being used. In most cases clients such scripts mostly falls into custom code approach but I have tried deeply and thoroughly to make it to work.

Other workaround may be by running some sql queries to update the db records. Otherwise, sadly we will be left with the option to manually update the posts relation.

Thank you

#534385

I understand. I exported the databases as csv and imported with All importer tool. Trust me, the id's are "correct" (old, sure and not accurate in new system), but as said, there really is no error there.

The problem now is that the script doesn's work as expected and I understand it's not your duty to make it work, I really appreciate your help and efforts on this Noman 🙂
For me the manual update is really not an option, there are thousands of relations I have to update. So I have to think something else. BTW anyone who can provide me a solution, please provide it here/contact me (I will pay of course).

Anyway if we find a solution I think this could help many in future, since I don't believe I'm the only one who wants to import a big relational db into toolset. Or perhaps they do it differently and I just didn't find the right way?

#534420

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your understanding and feedback 🙂 If you want to work with some dedicated person, we have some recommended list of service providers here who can help in such cases for you:
https://toolset.com/consultant/

I haven't yet come to a ticket or find one where any other solution is mentioned to this problem, otherwise it would be quite easy to try that one out.

#534483

Hi Noman, good news. I found a way, the All import plugin is excellent, there is a mapping possibility. So I can give new value to any old value.
Thanks again!
- Katja

The forum ‘Types Community Support’ is closed to new topics and replies.