'Musica', 8 => 'Pensamientos', 9 => 'Portadas', 10 => 'homilias_fray_marcos', 11 => 'stories', 12 => '1jornada_feadulta' ]; global $wpdb; foreach ($mappings as $folder_id => $relative_base) { echo "Processing Folder: $relative_base (ID: $folder_id)...\n"; $base_dir = '/var/www/html/wp-content/uploads/' . $relative_base; if (!is_dir($base_dir)) { echo "Directory not found: $base_dir\n"; continue; } $files = scandir($base_dir); $count = 0; $updated = 0; foreach ($files as $file) { if ($file === '.' || $file === '..') continue; $filepath = $base_dir . '/' . $file; if (is_dir($filepath)) continue; // Search for attachment with this filename (regardless of folder) // We look for %/file.mp3 $guid_search = '%' . $file; $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, guid FROM $wpdb->posts WHERE guid LIKE %s AND post_type = 'attachment'", $guid_search ) ); foreach ($attachments as $att) { // Check if it's in 2026/02/ or similar (not in target folder) if (strpos($att->guid, $relative_base . '/') === false) { // It is NOT in the target folder (e.g. it is in 2026/02/) // Move it back! $new_guid = str_replace( '/2026/02/', '/' . $relative_base . '/', $att->guid ); // Also handle other months if needed, but likely just 2026/02 based on date // Or construct GUID from site URL + uploads + relative path $upload_dir = wp_upload_dir(); $new_guid = $upload_dir['baseurl'] . '/' . $relative_base . '/' . $file; // Update GUID $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET guid = %s WHERE ID = %d", $new_guid, $att->ID ) ); // Update _wp_attached_file meta $new_attached_file = $relative_base . '/' . $file; update_post_meta( $att->ID, '_wp_attached_file', $new_attached_file ); // Delete the physical copy in 2026/02/ if exists // We need to parse the old path from old GUID or check _wp_attached_file before update // But for now, just update DB is enough to point to the correct file. // The correct file is in $filepath. echo "Updated ID $att->ID: $att->guid -> $new_guid\n"; $updated++; // Ensure assigned to FileBird folder $table_name = $wpdb->prefix . 'fbv_attachment_folder'; $current_folder = $wpdb->get_var( $wpdb->prepare( "SELECT folder_id FROM $table_name WHERE attachment_id = %d", $att->ID ) ); if ($current_folder != $folder_id) { if ($current_folder) { $wpdb->query( $wpdb->prepare( "UPDATE $table_name SET folder_id = %d WHERE attachment_id = %d", $folder_id, $att->ID ) ); } else { $wpdb->query( $wpdb->prepare( "INSERT INTO $table_name (folder_id, attachment_id) VALUES (%d, %d)", $folder_id, $att->ID ) ); } echo " Assigned to folder $folder_id\n"; } } else { // Already correct path // Check folder assignment $table_name = $wpdb->prefix . 'fbv_attachment_folder'; $current_folder = $wpdb->get_var( $wpdb->prepare( "SELECT folder_id FROM $table_name WHERE attachment_id = %d", $att->ID ) ); if ($current_folder != $folder_id) { if ($current_folder) { $wpdb->query( $wpdb->prepare( "UPDATE $table_name SET folder_id = %d WHERE attachment_id = %d", $folder_id, $att->ID ) ); } else { $wpdb->query( $wpdb->prepare( "INSERT INTO $table_name (folder_id, attachment_id) VALUES (%d, %d)", $folder_id, $att->ID ) ); } echo " Assigned ID $att->ID to folder $folder_id (path was OK)\n"; $updated++; } } } if (empty($attachments)) { // Import it if not found at all echo "Importing missing file: $file\n"; $cmd = "wp media import " . escapeshellarg($filepath) . " --porcelain --allow-root"; $output = shell_exec($cmd); $attachment_id = trim($output); if (is_numeric($attachment_id)) { $table_name = $wpdb->prefix . 'fbv_attachment_folder'; $wpdb->query( $wpdb->prepare( "INSERT INTO $table_name (folder_id, attachment_id) VALUES (%d, %d)", $folder_id, $attachment_id ) ); echo " Imported & Assigned ID $attachment_id\n"; $updated++; } } } echo "Completed $relative_base. Updated/Imported: $updated files.\n\n"; }