'How to save data into a custom database table instead of the postmeta table
I am trying to create an addon that works with WPALL import as explained here. https://www.wpallimport.com/documentation/addon-dev/structure/
I am using Metabox to create custom fields and custom post types and WP all Import to import the data.
I have successfully been able to import the data into the database but not the right table. Looks like the way it works is that the custom fields are by default saved to the database's postmeta table by the WPAll Import plugin.
How do I save the custom fields data I created into the custom table and not the postmeta?
include "rapid-addon.php";
final class custom_table_data_add_on {
protected static $instance;
protected $add_on;
static public function get_instance() {
if ( self::$instance == NULL ) {
self::$instance = new self();
}
return self::$instance;
}
protected function __construct() {
// Define the add-on
$this->add_on = new RapidAddon( 'Custom dbTable Add-On', 'custom_dbtable_data_addon' );
$this->add_on->add_field('fixtures_results', 'Fixtures & Results', 'text');
//$this->add_on->add_field('team_address', 'Team Address', 'text');
$this->add_on->set_import_function([ $this, 'import' ]);
add_action( 'init', [ $this, 'init' ] );
//Import
function import( $post_id, $data, $import_options, $article ) {
global $wpdb;
$fields = array(
'fixtures_results',
);
foreach ( $fields as $field ) {
// Make sure the user has allowed this field to be updated.
if ( empty( $article['ID'] ) || $this->add_on->can_update_meta( $field, $import_options ) ) {
$sql = "INSERT INTO {$wpdb->prefix}fixtures_results (post_id,fixtures_results) VALUES (%d,%s)
ON DUPLICATE KEY UPDATE fixtures_results = %s";
$sql = $wpdb->prepare($sql, $post_id, $field, $data[ $field ]);
$wpdb->query($sql);
}
}
}
}
}
custom_table_data_add_on::get_instance();
Notes * Link to rapidapi addon https://github.com/soflyy/wp-all-import-rapid-addon
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
