Skip Navigation

[Resolved] Export custom post type data to csv and include related post information

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

Problem: I have two custom post types in a one-to-many post relationship. Province is the parent (one) and District is the child (many). I would like to generate an export CSV that includes each District title and its parent Province title.

Solution: Go to wp-admin > All Export > New Export. The first step is to choose a specific post type, and choose District as that post type. Then click "customize export file" to move on to the next step. Usually ID, Title and Content are already in place in the builder. You can drag the ID and Content blocks out of the box since you don't want to include those items.

Next click "Add Field" below the builder area to add a new custom column for the Province Title. Choose the option to select a field to export, and select the ID field. You may need to reset the column name information after choosing the ID field. Check the checkbox to export a value from a PHP function, type prov_parent_title in the function name field, and paste this function in the function editor to define the custom prov_parent_title function:

[php]

Relevant Documentation:
https://toolset.com/forums/topic/export-custom-post-type-data-and-related-repeatable-field-group-data/

This support ticket is created 3 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by martink-8 3 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#2117931

Tell us what you are trying to do? Can I export custom post type data also contain post relationship field to csv . If yes ,which plugin/add-on can I use and how much does the it cost?
Is there any documentation that you are following?
No.
Is there a similar example that we can see?
No
What is the link to your site?
hidden link

#2118083

Hello, I'm not aware of any 3rd-party plugins that will export post relationship information automatically. When exporting post data from the standard WordPress > Tools > Export tool, the related post details are included automatically. This file format is a type of XML though, not CSV. You would need some conversion process to convert that file from XML to CVS automatically.

If the export plugin you choose allows custom PHP functions to create dynamic export fields, you can use the post relationships API to insert the related post information in the export document automatically. For example, WP All Export Pro allows you to use custom PHP functions to create dynamic fields in the export document.
https://www.wpallimport.com/upgrade-to-wp-all-export-pro/

I explained how to use the post relationships API in a custom function to export information about related posts with WP All Export Pro in another ticket here in the forum:
https://toolset.com/forums/topic/export-custom-post-type-data-and-related-repeatable-field-group-data/

In that example, the User is trying to export repeatable field groups (RFGs) and include information about their parent post. This is similar to exporting information about child posts in a O2M post relationship with another post type. If your post types are connected in a one-to-many post relationship (O2M), this example is very similar. If you have a different type of post relationship, like M2M for example, the setup is a bit different.

I could set up a similar example for you if you tell me more about the post relationship, and the post type you are trying to export.
- What type of post relationship is involved here - One to One(O2O)? One to Many (O2M)? Many to many (M2M)?
- Which post types are included in this relationship? What are the slugs of these post types?
- What are the roles of each post type - which is the parent and which is the child? You can find this information by editing the relationship in Toolset > Relationships. Check the checkbox "I understand that changes to these settings may delete post associations in this Relationship" and click "Edit settings" to see this role information.
- Which post type would you like to export?

#2120155

Sorry for the late reply. I have a 2 Post types.
I have already installed wp all Export pro plugin
The first one is province and the second is a district.
A province can have many Districts so it's a one to many relationship(02M). I want to export the districts post type with the following fields
1.title
2. province title(based on the post relationship)

#2120413
Screen Shot 2021-07-20 at 5.28.28 PM.png

Okay just to be clear, usually post titles alone are not enough to enable importing these posts in another site. Normally you need unique identifiers like post IDs or GUIDs. If you're exporting for a different purpose, then obviously whatever information you want to include is fine. For something simple like this you could probably use Views to generate this information, then copy + paste it into a simple text file and save that with a .csv extension. A View is usually the simplest practical approach when there is a relatively small amount of data to include, and that data can be easily output by Views using its dynamic content features. Otherwise you need to use custom PHP functions in the Export builder to fetch information from related posts.

If you want to use the exporter plugin, you'll go to wp-admin > All Export > New Export. The first step is to choose a specific post type, and choose District as that post type. Then click "customize export file" to move on to the next step. Usually ID, Title and Content are already in place in the builder. You can drag the ID and Content blocks out of the box since you don't want to include those items.

Next click "Add Field" below the builder area to add a new custom column for the Province Title. Choose the option to select a field to export, and select the ID field. You may need to reset the column name information after choosing the ID field. Check the checkbox to export a value from a PHP function, type prov_parent_title in the function name field, and paste this function in the function editor to define the custom prov_parent_title function:

<?php
function prov_parent_title($id)
{
  $parent_id = toolset_get_related_post( $id, 'relationship-slug', 'parent');
  $prov_title = get_the_title($parent_id);
  return $prov_title;
}
?>

Replace relationship-slug in this code with the slug of your Province-District post relationship. See the screenshot here for an example.

That's pretty much all you need to configure in the builder. Continue with the remaining default configurations and generate your export. Let me know if you have questions about this.

#2120739

My issue was resolved. Thank you!