Skip Navigation

Importing Content From CSV, with Post Relationships in WordPress

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:

IDpost_authorpost_title…(all other post fields)…custom_postmeta_keyanother_postmeta_key
1adminHello WorldCustom Postmeta ValueAnother 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:

_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 casePostmeta value
Single connected parent postparent_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.

TeachersKids
Mr. ATom
Mrs. BTom
Mr. ABrian

These associations exported to CSV file would look like:

IDpost_authorpost_titlepost_type…(all other post fields)…_toolset_associations_school-class
1adminMr.Ateachers…(all other post fields values)… 
2adminMrs.Bteachers…(all other post fields values)… 
3adminTomkids…(all other post fields values)“[Mr.A], [Mrs.B]”
4adminBriankids…(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 ToolsetExport / Import Associations tab:

List of associations

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.

Importing associations

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:

Example Output
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:

Association function return
Array ( 
	'success' => array( /* array of successfully imported associations */ ),
	'error' => array( /* array of associations, which could not be imported */ ),
)
Updated
November 16, 2020