Skip Navigation

[Resolved] Setting Post ID with custom field for import

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

Problem:

I am trying to import two spreadsheets as two separate, but related CPT's, and the two options that I have for connecting a related CPT is either by inputting the Post Title, or Post ID of the related post. Post Title is not possible to use for me since there are a bunch of Post Title's that are exactly the same wording and it will create anomalies, so I need to work with the Post ID.

Solution:

You might consider custom codes, for example:

https://toolset.com/forums/topic/setting-post-id-with-custom-field-for-import/#post-2246647

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 3 years 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by simchaH 3 years ago.

Assisted by: Luo Yang.

Author
Posts
#2246279

Hi,

I am trying to import two spreadsheets as two separate, but related CPT's, and the two options that I have for connecting a related CPT is either by inputting the Post Title, or Post ID of the related post. Post Title is not possible to use for me since there are a bunch of Post Title's that are exactly the same wording and it will create anomalies, so I need to work with the Post ID.

My issue starts with the fact that I cannot obtain Post ID's before creating the Post itself. So I thought of a solution- I create a column called "ID" on the spreadsheet that uniquely identifies each row/post in the sheet (just like a Post ID, only it's a toolset custom field created by me).

My question is, if I import spreadsheet A with this extra column of "ID", is there a piece of code that can then take my custom made ID, and put it into the Post ID, and swap out the default ID that the Post is created with? This way, when I import spreadsheet B and want to connect them, I can simply use the "ID" column I created, because it will then be the actual Post ID's of spreadsheet A.

Thank You!

#2246647

Hello,

There isn't such kind of built-in feature within Toolset plugins, see our document:
https://toolset.com/course-lesson/how-to-import-content-into-wordpress-using-csv/
Below two CVS importer plugins support Toolset post type replationship:
- WordPress Ultimate CSV Importer
- WP All Import – Toolset Types Add-On plugin
Both of them use other post's title to connect posts:
https://toolset.com/course-lesson/how-to-import-posts-from-csv-files-and-maintain-relationships-when-using-the-csv-importer-plugin/
https://toolset.com/course-lesson/import-posts-from-csv-with-relationships-using-wp-all-import/

In your case, you might consider custom codes, for example:
1) In your CSV file, add a custom field column "related-post-id", store the related post IDs

2) When you use CSV importer plugin to import data, use action hook "save_post" to trigger a custom PHP function:
https://developer.wordpress.org/reference/hooks/save_post/

3) In this PHP function, get the custom field "related-post-id" value:
https://developer.wordpress.org/reference/functions/get_post_meta/
And use it to related with other posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

#2251577

My issue is resolved now. Thank you!