Skip Navigation

[Resolved] Many to Many relationship Import using CSV

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

Problem:
How could we import data in relationships, such as a Many to Many Relationship?

Solution:
The best to get comfortable with the several methods of import available is to read thru this comment:
https://toolset.com/forums/topic/many-to-many-relationship-using-csv/#post-1598889

It distinguishes the different methods and adds specific hints for each.

Relevant Documentation:
https://toolset.com/documentation/user-guides/export-import/how-to-import-content-into-wordpress-using-csv/

This support ticket is created 4 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 5 replies, has 2 voices.

Last updated by JamesS2731 4 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#1598117

Tell us what you are trying to do? I'm trying to create a many to many relationship between two CPT's; Members and Members Cars.

The Members fields include CarID and MembershipID. The Members Cars fields include CarID.

I have been given a csv which just contains two columns: MembershipID and CarID. (see attached)

Members can have owned more than one car and a car can have had more than one owner.

I have WP All Import installed as well if required.

Whats the best way of importing the csv and auto-creating the relationships?

Is there any documentation that you are following? I did find https://toolset.com/documentation/user-guides/export-import/importing-content-from-csv-with-post-relationships/ but wasn't sure how it would help as I only have the two fields mentioned in the csv.

Is there a similar example that we can see? Unfortunately not.

What is the link to your site? hidden link

Any thoughts or ideas most welcomed.

Kind regards
James

#1598889

You have a few options to import relationships

Start here:
https://toolset.com/documentation/user-guides/export-import/how-to-import-content-into-wordpress-using-csv/

As you can see, 2 options jump out immediately for any Post Relationship import because the 3rd does not support this at all natively:
WordPress Ultimate CSV Importer and WP All Import – Toolset Types Add-On plugin
The best option is WordPress Ultimate CSV Importer because it already supports Repeatable Field Groups as well.

Those 2 Plugins developed special approaches to the task and hence follow each a particular workflow.
WordPress Ultimate CSV Importer:
https://toolset.com/documentation/user-guides/export-import/how-to-import-content-into-wordpress-using-csv/import-csv-ultimate-csv-importer-plugin/
Details about Post Relationships:
https://toolset.com/documentation/user-guides/export-import/how-to-import-content-into-wordpress-using-csv/import-csv-ultimate-csv-importer-plugin/how-to-import-posts-from-csv-files-and-maintain-relationships-when-using-the-csv-importer-plugin/

WP All Import – Toolset Types Add-On plugin
https://toolset.com/documentation/user-guides/export-import/how-to-import-content-into-wordpress-using-csv/how-to-import-data-from-a-csv-file-using-the-wp-all-import-toolset-types-add-on-plugin/
Details about Post Relationships:
https://toolset.com/documentation/user-guides/export-import/how-to-import-content-into-wordpress-using-csv/how-to-import-data-from-a-csv-file-using-the-wp-all-import-toolset-types-add-on-plugin/post-relationships/

Both of these documentations are very clear and hold applicable examples. You will, however, need to amend your CSVs to match what those plugins expect.
The documentation also features sample CSV.

Now, https://toolset.com/documentation/user-guides/export-import/importing-content-from-csv-with-post-relationships/ describes the Syntax which Toolset Types expects from a Post in general, in order to trigger an Import opportunity in Toolset → Export/Import.
It describes how to create a CSV with a Column that will be interpreted as a Custom Field by any CSV importer you use.
That column (custom field) should hold for each row (post) the Title or GUI of the related post.
Hence again here we expect a concise structure, and there is no way to "circumnavigate" this. The CSV must be set up exactly like exampled with the data as shown in the DOC.

When such CSV is imported, it will then create posts with that Custom Field and each post will hold as the value in that Custom Field the related posts.

Types now can recognize this and will trigger the import opportunity in Toolset → Export/Import where you can finalize the relationship import.

This is - note - when you do not use any of the previously mentioned 2 plugins which offer specific integration.
Because those, already come with an inbuilt and proprietary method, as you will see on their examples, their CSV are structured differently.

The syntax described in https://toolset.com/documentation/user-guides/export-import/importing-content-from-csv-with-post-relationships/ is onl for plugins that will actually create a Custom Field of that Column, if the import does not do that, it won't work.
This is important.
Most CSV Importers follow that exact structure, so it should not be a problem, but it's important to understand that we are attempting to create a Custom Field here.

The importers generally will expect, that the CSV file will feature a column with heading as the Custom Field Name (key) should be.
In our case that will be _toolset_associations_POST-RELATIONSHIP-SLUG.
You should replace the POST-RELATIONSHIP-SLUG part with the actual slug of your Post relationship.

For example, if you have a relationship "teacher-classes" you would use _toolset_associations_teacher-classes as the column title.
This will then be interpreted as a Custom Field by your CSV importer (most CSV importer works like that)
It will store it in the database for the post which it belongs to in the row.

The importer plugins then generally will expect that for the Custom Field Column just created we add values for each row (post)
In our case, this value must be either the Title or the GUID of the related post.

So, as the value in the column where it intersects with the post row, you will add either the title of the one related post or many titles, of many related posts, as the syntax on the DOC describes.

Possible values and mandatory syntax
1. For one related post:
Parent Post Title
2. For a related post and an intermediary:
{!{Parent Post Title}!} + {!{Intermediary Title}!}
3. For many related posts:
{!{Parent Post Title}!}, {!{Parent Post Title}!}, (etc etc)

What you need to do when populating the CSV is making sure to replace above "Parent Post Title" with the actual title of your "parent" post. But do not remove the {!{}!}, when adding many posts. It's required syntax.

Each row in your CSV will now have a distinct value for the _toolset_associations_POST-RELATIONSHIP-SLUG column. Each value should be formatted like above shown and hold either Parent Post Title or GUID

Now, when you import these Posts with any importer, it creates the posts and adds the Custom Fields as we specified.

This is outlined nicely in this example:
https://toolset.com/documentation/user-guides/export-import/importing-content-from-csv-with-post-relationships/#an-example-of-how-a-csv-would-look-like > These associations exported to CSV file would look like:
That's how your CSV should look like to be ready for the relationship import.

This is valid if you use any Importer unless WP Ultimate CSV Importer Pro or WP All Import – Toolset Types Add-On plugin

I think you will have to amend your CSV according to the above structures

#1601903

Many thanks Beda, that's great. Took a while to get the extra fields added to our spreadsheet but used a pivot table in the end and now all ready for import.

Best regards
James

#1603243

Hi Beda,

Apologies, just a quick question. When importing the relationship, is it only the post title field that will work to create the relationship?

In our example, the CarIDs are the fields that we want to use to create the relationship but the Members Cars post type uses the chase number as the post title.

We can amend the csv file but I wanted to check before doing so.

Best regards
James

#1603665

If you follow this structure, https://toolset.com/documentation/user-guides/export-import/importing-content-from-csv-with-post-relationships/, you'd need to use either Title, or GUID
See https://toolset.com/documentation/user-guides/export-import/importing-content-from-csv-with-post-relationships/#how-toolset-associations-can-be-added-to-a-csv:

The title of the parent/intermediary is always surrounded by {!{ … }!} to support any signs in the title.

There is an exception in associations for one-to-one relationships, where only one parent post can be associated, surroundings are not required, but also don’t hurt.

Instead of using the title of the post, you can also use the GUID. This is helpful for the case you do not have unique titles

(See here the definition of a GUID: hidden link)

I hope this helps!

#1604329

Many thanks Beda (and apologies for the multiple tickets!). We swapped field data for post taels and successfully imported all relationships.

Thanks, as always for your help. Much appreciated.

Cheers
James