My code has saved the attachment file in the WP media library. Now I want to save the attachment ID in my custom table. How can I do that? I have saved attachment ID in my table with a single upload file but I don't know how to save multiple attachment id in custom table? Because there are used for-each loop for media upload. Thanks!
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$files = $_FILES["my_image_upload"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("my_image_upload" => $file);
//var_dump($_FILES);
foreach ($_FILES as $file => $array) {
// $newupload = my_handle_attachment($file,$post_id);
$attach_id = media_handle_upload( $file, -1 );
var_dump($attach_id);
var_dump($array);
}
}
}
use
global $wpdb
$wpdb->insert( $table, $data, $format );
$wpdb->insert_id
table (string) The name of the table to insert data into.
data (array) Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
format (array|string) (optional) An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data. If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.
you can also run a something like
global $wpdb;
$sql = "INSERT INTO table (xxx, xxx, xxx, xxx, xxx) VALUES ('$xxx', '$xxx', 'xxx', '$xxx', CURRENT_TIMESTAMP)";
$wpdb->query($sql)
here you can find more examples https://codex.wordpress.org/Class_Reference/wpdb