Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trey8611/036e34c6d14f7235a1ffb1e8f2bd204b to your computer and use it in GitHub Desktop.
Save trey8611/036e34c6d14f7235a1ffb1e8f2bd204b to your computer and use it in GitHub Desktop.
Import Data to JetEngine Relations

How to Import JetEngine Relations

This snippet will work for One to One, One to Many and Many to Many relationships:

Instructions:

  • Create a "_temp_child" temporary field to store the values of the relations you want to import (see https://d.pr/i/cIooGf). It will accept multiple values separated with commas, and you can use either the ID or the Title.
  • Edit the snippet using the instructions below and paste it in the Function Editor inside WP All Import.
  • The "jet_rel_default" table is the default table used for relations in JetEngine. If you're using a separate table, you'll need to set that table's name instead.
  • You'll need to specify the relationship ID in 'rel_id’; you can find it here (see https://d.pr/i/eSVZ32).
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment