Skip Navigation

[Resolved] Inserting custom posts via PHP; help with post reference (dropdown)

This support ticket is created 3 years, 5 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by mustafaA-2 3 years, 5 months ago.

Assisted by: Jamal.

Author
Posts
#2124287

Tell us what you are trying to do?
-I have a couple of customs posts( certificaten, cursisten).
Users are able to upload a CSV/Excel file to add their cursisten with all the information.

The cursisten post type has custom fields, one of them is a post reference to certificaten.

If i upload the data now, it skips the dropdown (post reference) and leaves it empty; how can i insert the data the correct keeping in mind the values from the post reference?

Is there any documentation that you are following?
- n/a
Is there a similar example that we can see?
-Not that i know of.
What is the link to your site?
-private

#2124423

Hello and thank you for contacting Toolset support.

If I understood well, you are importing data from CSV files using a plugin, right? What plugin?

Keep in mind that a Post Reference Field is stored as a hidden relationship instead of a regular custom field. The plugin that you are using needs to support importing relationships, and the field needs to e imported as a related post.

If you are using only Toolset without any plugin, follow this article, and the section related to relationships. Keep in mind that the post reference field's slug is the slug of the hidden relationship.
https://toolset.com/course-lesson/importing-content-from-csv-with-post-relationships/

If you are using WP All Import, or WP Ultimate CSV Importer Pro, follow these articles:
- https://toolset.com/course-lesson/import-posts-from-csv-with-relationships-using-wp-all-import/
- https://toolset.com/course-lesson/how-to-import-posts-from-csv-files-and-maintain-relationships-when-using-the-csv-importer-plugin/

I hope this helps. Let me know if you have any questions.

#2124425

Reading this ticket's title, I assume that you are using your own PHP code, right? If that's the case, check the toolset_import_associations_of_child function in this article https://toolset.com/course-lesson/importing-content-from-csv-with-post-relationships/

#2124547

Hi! Thanks for your quick reply.

I am indeed using my own PHP code for the uploadform, the upload and the processing of the CSV and Excel files.

So that would leave me with the last link you posted in the 2nd reply, not sure how i should go on about that.

Do you have any examples?

#2124601

Unfortunately, I could not find any example for that function. Let me try it on my test site and get back to you asap.

#2124991

Hi Jamal,

I took a look in the database to see if I could find any way it's all linked to each other but unfortunately I couldn't. If you could help with that I can probably create a custom query for that one field, which is also fine for now.

What I've got at the moment is:

add_post_meta($postID, 'wpcf-behaalde-certificaat', $certificaat, true);

Where 'wpcf-behaalde-certificaat' is the custom field with post reference.

This does add the value from the CSV file (if i insert it into a regular field it shows up fine) but I can't find it in the database if i import it into the field i need (the post reference field)

#2125121

I managed to fix it using manual queries. For now there is a set of 'Certificaten' so i hardcoded that;

//First i got the Post IDs 
//BHV = 6; EHBO= 8; Heftruck= 2371; Reachtruck = 2693; Verreiker = 7555;

//I looked up the group ID belonging to the post ID above and inserted it in an array
$certificatenIDs['BHV'] = 12;
$certificatenIDs['EHBO'] = 8;
$certificatenIDs['Heftruck'] = 20;
$certificatenIDs['Reachtruck'] = 10;
$certificatenIDs['Verreiker'] = 24;

//$certificaat is from the excel column; lookup key in array and get value
$certType = $certificatenIDs[$certificaat];

//Not sure how this works :(
//$test = toolset_connect_posts( 'certificaat-cursist', $certType, $postID);

//Needed for queries
global $wpdb;

//Random groupID for the cursist post type (consist of PostID of cursist and the groupID of the Certificates)
$groupid = $postID.$certType;


$wpdb->insert('lFArioRUXm_toolset_connected_elements', array(
'element_id' => $postID,
'domain' => 'posts',
'wpml_trid' => '0',
'lang_code' => '',
'group_id' => $groupid
));

$wpdb->insert('lFArioRUXm_toolset_associations', array(
'relationship_id' => '2',
'parent_id' => $certType,
'child_id' => $groupid,
'intermediary_id' => '0'
));

I'd still like a better solution so I don't need to constantly update the hardcoded IDs.

I could probably get all IDs where post_type = 'certificate'; and then lookup their groupID and insert all that in the array; I'd rather have a clean Toolset way to do this.

#2125135

I wouldn't recommend inserting entries directly into the database. You should use Toolset API for that mean. I did not test the toolset_import_associations_of_child function yet. But you can also use the toolset_connect_posts function, which should create the entries in Toolset tables.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

I'll check about toolset_import_associations_of_child today, as soon as I can and will get back to you.

#2125971

My issue is resolved now. Thank you!