prefix . 'fbv_attachment_folder'; foreach ($folders as $name) { $base_dir = '/var/www/html/wp-content/uploads/' . $name; if (!is_dir($base_dir)) continue; $files = scandir($base_dir); $count = 0; foreach ($files as $f) { if ($f === '.' || $f === '..') continue; $filepath = $base_dir . '/' . $f; if (is_dir($filepath)) continue; $guid_search = '%' . $name . '/' . $f; $att_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid LIKE %s AND post_type = 'attachment'", $guid_search)); if (!$att_id) { $cmd = "wp media import " . escapeshellarg($filepath) . " --porcelain --allow-root"; $out = shell_exec($cmd); $att_id = trim($out); if (is_numeric($att_id)) { $wpdb->query("INSERT INTO $fbv_att (folder_id, attachment_id) VALUES ($folder_id, $att_id)"); $count++; } } } echo "$name: $count files\n"; } echo "DONE\n";