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/ce642d83661f4456043b33f5aefe9696 to your computer and use it in GitHub Desktop.
Save trey8611/ce642d83661f4456043b33f5aefe9696 to your computer and use it in GitHub Desktop.
Export JetEngine Relations with WP All Import

How to Export JetEngine Relations

This function will use the default "jet_rel_default" table in JetEngine to look for the post_id of the parent and return its relations from the "child_object_id" column. If you're using a separate table for your relations, you can change the $table_name value.

Instructions:

  • Add a New Field in WP All Export (see https://d.pr/i/OW9dze).
  • Set a name for the column and use "ID" for the field to export (see https://d.pr/i/v5Mq98).
  • Modify the function as needed, paste it in the Function Editor and paste the name of the function in the "Export the value returned by a PHP function" field (see https://d.pr/i/2c6c07).
  • The function will return a list of IDs separated with pipes by default, but if you want to export the titles instead, you need to change the $mode in "function my_export_je_relation ($post_id, $mode = 'id')" to "title".
// Change "id" for "title" if you need to export the titles instead of IDs
function my_export_je_relation ($post_id, $mode = 'id') {
    global $wpdb;

    // 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";

    // Fetch child object IDs related to the parent post ID.
    $child_ids = $wpdb->get_col($wpdb->prepare("SELECT child_object_id FROM $table_name WHERE parent_object_id = %d", $post_id));

    // Default behaviour is to return the ID, but you can change the mode to "title" to export titles instead 
    if ('title' === $mode) {
        // Get relations titles by ID
        $titles = array_map(function($id) {
            $post = get_post($id);
            return $post ? $post->post_title : '';
        }, $child_ids);

        return implode('|', $titles);
    }

    return implode('|', $child_ids);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment