function my_import_je_relations ($id) {
global $wpdb;
// Change "_temp_child" to the temp field you set up in Custom Fields
$child_identifiers = get_post_meta($id, '_temp_child', true);
// Check if there are multiple identifiers in '_temp_child', separated by commas.
$child_identifiers = explode(',', $child_identifiers);
// The "jet_rel_default" table is the default JetEngine value, if your using a custom DB table for relations, change it here
$table_name = $wpdb->prefix . "jet_rel_default";
// Loop through as many "_temp_child" elements there are inserting them in the DB as new rows
foreach ($child_identifiers as $child_identifier) {
// Trim spaces and check if the identifier is numeric or a title.
$child_identifier = trim($child_identifier);
$child_id = false;
// Checks if the _temp_child field is numeric, meaning an ID
if (is_numeric($child_identifier)) {
$child_id = $child_identifier;
} else {
// If it's not numeric, assume it's a title and look up the corresponding ID.
// Replace 'post-child' with your actual post type.
$child_post = get_page_by_title($child_identifier, OBJECT, 'post-child');
if ($child_post) {
$child_id = $child_post->ID;
}
}
if ($child_id) {
// Insert the relationship ID in 'rel_id', and the Parent relationship in 'parent_rel'
$data_to_insert = array(
'parent_object_id' => $id,
'child_object_id' => $child_id,
'rel_id' => 2,
'parent_rel' => 0
);
$format = array('%d', '%d', '%d', '%d');
$wpdb->insert($table_name, $data_to_insert, $format);
}
}
delete_post_meta($id, '_temp_child');
}
add_action('pmxi_saved_post', 'my_import_je_relations', 10, 1);