Types allows you to import content from CSV files, including relationship information. You will be able to create CSV files that include both the content to import, as well as the connections between posts.
The connection information will first be imported into post-meta. Then, Types will use it to build the connections table. This guide explains how to structure the content import and how to process the connections in Types.
How CSV Importers work
Let’s say you have a CSV Importer. Basically it’s importing data using such CSV format:
ID | post_author | post_title | …(all other post fields)… | custom_postmeta_key | another_postmeta_key |
---|---|---|---|---|---|
1 | admin | Hello World | … | Custom Postmeta Value | Another Postmeta Value |
It ports all data from the wp_posts table and also all postmeta related to the post. Where the key on the CSV is the same key as the postmeta itself. This way any postmeta can be added to the CSV and it will be imported by that key.
How Toolset Associations can be added to a CSV
An association has at least 3 parts and can have 4 (when it involves an intermediary post):
- Relationship (to which the associations belongs to)
- Parent Post
- Child Post
- Intermediary Post (only for m2m relationship with custom fields)
To move this data to postmeta we always use the child post to collect the associations. For the CSV file, this means that the child post holds the association data.
The relationship is stored in the key of the postmeta, with the following pattern:
1 | _toolset_associations_%relationship_slug% |
Where %relationship_slug%
is replaced by the actual slug of the relationship (without the %). For example _toolset_associations_flights
, where flights
is a relationship between Airports and Planes.
The parent and intermediary Posts (if available) are stored as the value of the postmeta. If a child post has multiple associations, the associations are separated by ,
. Here a list of possible values:
Use case | Postmeta value |
---|---|
Single connected parent post | parent_title |
Single connected parent post with an intermediary post | [parent_title] + [intermediary_title] |
Multiple parent posts | [parent_1_title], [parent_2_title], … |
Multiple parent posts with an intermediary post | [parent_1_title] + [intermediary_1_title], [parent_2_title] + [intermediary_2_title], … |
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.
An example of how a CSV would look like
Let’s say we have a m2m relationship School Class with the slug school-class. It connects parents Teachers and children Kids.
These are our associations, where “Mr. A”, “Mrs. B”, “Tom” and “Brian” are the titles of the posts.
Teachers | Kids |
---|---|
Mr. A | Tom |
Mrs. B | Tom |
Mr. A | Brian |
These associations exported to CSV file would look like:
ID | post_author | post_title | post_type | …(all other post fields)… | _toolset_associations_school-class |
---|---|---|---|---|---|
1 | admin | Mr.A | teachers | …(all other post fields values)… | |
2 | admin | Mrs.B | teachers | …(all other post fields values)… | |
3 | admin | Tom | kids | …(all other post fields values) | “[Mr.A], [Mrs.B]” |
4 | admin | Brian | kids | …(all other post fields values)… | Mr. A |
You see only the children posts are holding the associations informations and not the parents posts. Once this data is imported by any CSV Importer as postmeta you will see the associations on the Toolset → Export / Import Associations tab:
Once Start Import is clicked the associations will be written to the toolset associations database table and are ready to use. If you have associations in your CSV, which already exists you will also see that on the import screen. This means there will not be any issues if you import associations multiple times as existing will be ignored.
API functions
We provide two functions regarding associations export / import.
toolset_export_associations_of_child( $child_id )
This function will return an array of associations by $child_id.
Regarding our School Class example: toolset_export_associations_of_child( 3 ) would return:
1 2 3 | Array ( [_toolset_associations_school-class] => [http://test.loc/?post_type=teacher&p=1], [http://test.loc/?post_type=teacher&p=2] ) |
The function is using the parents/intermediary GUID instead of the title.
toolset_import_associations_of_child( $child_id )
This function will import associations stored in the postmeta of $child_id.
It will return an array in this format:
1 2 3 4 | Array ( 'success' => array( /* array of successfully imported associations */ ), 'error' => array( /* array of associations, which could not be imported */ ), ) |
Related topics
- Bulk Editing Toolset Content with WP Sheet Editor
- Import posts from CSV files and maintain relationships with WP All Import
- Import content from CSV files with post relationships using WP Ultimate CSV Importer Pro
- Importing repeatable field groups when using the CSV Importer plugin
- Import Data using the WP All Import Plugin
- Import Content Using CSV Importer Plugin
- Import Data using the WP Ultimate CSV Importer plugin
- How to Import Content Using CSV Files