$file_url = 'https://rkb-odisha.in/sites/default/files/2019-11/Ajay-Rice-Paddy-Variety-Odisha-Odia.pdf';

// Download the file to a temporary location
$tmp_file = download_url($file_url);

// Check if the file was downloaded successfully
if (is_wp_error($tmp_file)) {
// There was an error downloading the file
error_log('Download error: ' . $tmp_file->get_error_message());
} else {
// Prepare the array needed for media_handle_sideload()
$file_array = [
'name' => basename($file_url), // Filename (basename strips the URL to get the file name)
'tmp_name' => $tmp_file // The temporary file location returned by download_url()
];

// Optionally, associate it with a post (replace with your post ID)
$post_id = 1540; // Replace with actual post ID if you want to associate the file with a post

// Upload the file to the WordPress media library
$attachment_id = media_handle_sideload($file_array, $post_id);

// Check for errors
if (is_wp_error($attachment_id)) {
// There was an error uploading the file
error_log('Upload error: ' . $attachment_id->get_error_message());
@unlink($tmp_file); // Clean up the temporary file
} else {
// File uploaded successfully, get the URL of the attachment
$file_url = wp_get_attachment_url($attachment_id);
echo 'File uploaded successfully! URL: ' . $file_url;

// Prepare post data for the attachment (PDF metadata)
$attachment_data = array(
'ID' => $attachment_id,
'post_excerpt' => 'Your PDF Caption', // Caption (optional)
'post_content' => 'Your PDF Description', // Description (optional)
);

// Update the attachment post data
wp_update_post($attachment_data);

// Update ACF field with $attachment_id with that $post_id
update_field('download_en', $attachment_id, $post_id);
}

// Clean up the temporary file after upload
@unlink($tmp_file);
}

// Update to all custom field data

// Post ID for which you want to update the custom fields
$post_id = 123; // Replace with actual post ID

// Taxonomy fields (update these with actual term IDs)
$district = 45; // Replace with the actual term ID for District
$ecology = 12; // Replace with the actual term ID for Ecology
$season = 34; // Replace with the actual term ID for Season
$stress_condition = 67; // Replace with the actual term ID for Stress Condition
$duration = 89; // Replace with the actual term ID for Duration

// Set taxonomy terms (these need to be term IDs or slugs)
wp_set_object_terms($post_id, $district, 'district');
wp_set_object_terms($post_id, $ecology, 'ecology');
wp_set_object_terms($post_id, $season, 'season');
wp_set_object_terms($post_id, $stress_condition, 'stress_condition');
wp_set_object_terms($post_id, $duration, 'duration');

// Text and textarea fields
update_field('suitable_ecology', 'Lowland', $post_id); // Suitable Ecology
update_field('suitable_season', 'Kharif', $post_id); // Suitable Season
update_field('plant_height', '120 cm', $post_id); // Plant height
update_field('grain_type', 'Medium Slender', $post_id); // Grain Type
update_field('days_to_50_flowering', '110', $post_id); // Days to 50% flowering
update_field('abiotic_stress_tolerance', 'Drought', $post_id); // Abiotic Stress Tolerance
update_field('biotic_stress_tolerance', 'Bacterial Blight', $post_id); // Biotic Stress Tolerance
update_field('special_feature', 'High Yielding', $post_id); // Special Feature
update_field('potential_yield_kg_ha', '5000', $post_id); // Potential yield (Kg/ha)
update_field('bst', 'Yes', $post_id); // BST
update_field('duration2', 'Short Duration', $post_id); // Duration2

// Handling file uploads for Download(En) and Download(Or)
$file_url_en = 'https://example.com/path-to-file-en.pdf';
$file_url_or = 'https://example.com/path-to-file-or.pdf';

// Function to handle file upload from URL
function upload_file_from_url($file_url, $post_id)
{
// Download file to temp location
$tmp_file = download_url($file_url);

// Check for download errors
if (is_wp_error($tmp_file)) {
error_log('Download error: ' . $tmp_file->get_error_message());
return false;
}

// Prepare file array for upload
$file_array = [
'name' => basename($file_url), // Get filename
'tmp_name' => $tmp_file
];

// Handle the file upload and sideload it into the media library
$attachment_id = media_handle_sideload($file_array, $post_id);

// Check for errors
if (is_wp_error($attachment_id)) {
error_log('Upload error: ' . $attachment_id->get_error_message());
@unlink($tmp_file); // Clean up temp file
return false;
}

// Get the URL of the uploaded file
$file_url = wp_get_attachment_url($attachment_id);

// Clean up the temporary file
@unlink($tmp_file);

return $file_url;
}

// Upload files and update custom fields
$file_url_en_uploaded = upload_file_from_url($file_url_en, $post_id);
$file_url_or_uploaded = upload_file_from_url($file_url_or, $post_id);

// Update the custom fields for file URLs if uploads were successful
if ($file_url_en_uploaded) {
update_field('download_en', $file_url_en_uploaded, $post_id); // Download(En) field
}
if ($file_url_or_uploaded) {
update_field('download_or', $file_url_or_uploaded, $post_id); // Download(Or) field
}