Support Forum
Support › MB Relationships › Import/Export custom post with relationship field
I'm trying to bulk import/export thousands of posts in a CSV format, they have a relationship between them. I have tried using WP All Export and Ultimate CSV, they capture all the fields, but not the relationship field. May I know how we can ensure that the relationship field is captured on import/expoprt?
Hi Mark,
The extension MB Relationships saves data in a custom table, please read more here https://docs.metabox.io/extensions/mb-relationships/#database
You can try to create the code to import data to the custom table, here are some tuts
https://code.tutsplus.com/tutorials/custom-database-tables-importing-data--wp-28869
https://wordpress.org/support/topic/import-to-custom-table/
There is still no import-export solution for repeater and relationship fields?
Sorry Guys, I can't understand why you are the only ones for whom there is an import export solution for the field types.
Hello Metabox. I think you really should add an import/export .CSV that support the relationship.
It's such a gigantic work to do by hand.
Is it possible to do that with WP Ultimate CSV Importer ?
I aked chatgpt to add that feature into wordpress. I don't know if it can works. Gotta try it now.
<?php
/*
Plugin Name: Metabox Relationships Export/Import
Description: Export and import Metabox Relationships data in JSON format.
Version: 1.0
Author: Your Name
*/
// Export Metabox Relationships data as JSON
function export_metabox_relationships_as_json() {
global $wpdb;
// ... (same export code as before) ...
// Output the JSON for download
header('Content-disposition: attachment; filename=metabox_relationships_export.json');
header('Content-type: application/json');
echo $json_data;
exit;
}
// Import Metabox Relationships data from JSON
function import_metabox_relationships_from_json() {
if (isset($_FILES['metabox_relationships_json'])) {
$json_data = file_get_contents($_FILES['metabox_relationships_json']['tmp_name']);
// Decode JSON data
$import_data = json_decode($json_data, true);
if ($import_data) {
// Process and import relationships data here
// ... (import code) ...
// Ensure data validation and use Metabox Relationships API to import
} else {
// Handle JSON decoding error
echo "Invalid JSON data!";
}
}
}
// Create admin menu items for export/import
function add_metabox_relationships_menu() {
add_menu_page(
'Metabox Relationships Export',
'Metabox Relationships',
'manage_options',
'metabox-relationships-export',
'export_metabox_relationships_as_json'
);
add_submenu_page(
'metabox-relationships-export',
'Import Metabox Relationships',
'Import',
'manage_options',
'metabox-relationships-import',
'import_metabox_relationships_page'
);
}
// HTML for import page
function import_metabox_relationships_page() {
?>
<div class="wrap">
<h1>Import Metabox Relationships</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="metabox_relationships_json">
<?php submit_button('Import Relationships'); ?>
</form>
</div>
<?php
}
// Hook functions to admin menu and actions
add_action('admin_menu', 'add_metabox_relationships_menu');
add_action('admin_post_import_metabox_relationships', 'import_metabox_relationships_from_json');
// the export code should be this one. (can't edit post here)
// Change these table names if needed
$table_relationships = $wpdb->prefix . 'mb_relationships';
$table_relationships_items = $wpdb->prefix . 'mb_relationships_items';
$relationships_data = $wpdb->get_results("
SELECT * FROM $table_relationships
JOIN $table_relationships_items ON $table_relationships.ID = $table_relationships_items.relationship_id
", ARRAY_A);
// Prepare data in a suitable format for export
$export_data = array();
foreach ($relationships_data as $relationship) {
$relationship_id = $relationship['relationship_id'];
// Group relationships by their ID
if (!isset($export_data[$relationship_id])) {
$export_data[$relationship_id] = array(
'id' => $relationship_id,
'name' => $relationship['name'],
'type' => $relationship['type'],
'description' => $relationship['description'],
'items' => array()
);
}
// Add items related to this relationship
$export_data[$relationship_id]['items'][] = array(
'id' => $relationship['item_id'],
'type' => $relationship['item_type'],
// Add other item fields as needed
);
}