<?php
$upload_dir = wp_upload_dir();
$csv_path = $upload_dir['basedir'] . '/talia.csv';

if (file_exists($csv_path)) {
    $handle = fopen($csv_path, 'r');
  // Process CSV
$headers = fgetcsv($handle); // Read headers
        while (($row = fgetcsv($handle)) !== FALSE) {
            $data = array_combine($headers, $row);
     if (empty($data['Title'])) continue; // Skip rows without a name
            // Create post
            $post_id = wp_insert_post([
                'post_title'   => $data['Title'],
                'post_status'  => 'publish',
                'post_type'    => 'listdom-listing'
            ]);   
            
            if (is_wp_error($post_id)) continue;
            
    //echo"   fgf333d";
            // Insert address and phone as separate postmeta rows
            add_post_meta($post_id, 'lsd_address', $data['Address']);
            add_post_meta($post_id, 'lsd_phone', $data['Phone']);
    
            // Handle category
        $category = sanitize_text_field($data['Category']);
      
   $terms = $data['Category'];
   $taxonomy = 'listdom-category';
   $append = false;
   
   $result = wp_set_object_terms($post_id, $terms, $taxonomy, $append);
if (is_wp_error($result)) {
    echo 'Error: ' . $result->get_error_message();
} else {
    echo 'Success! Term taxonomy IDs: ' . implode(', ', $result);
    echo "<br>";
}
}
 
    
}