Advertisement
ellegi

Untitled

Apr 12th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1. <?php
  2. require_once('wp-load.php');
  3.  
  4. $posts_per_batch = 1000;
  5. $last_imported_id = get_option('last_imported_id', 0);
  6. global $wpdb;
  7. $table_name = 'screaming_export';
  8.  
  9.    
  10. $query = "SELECT * FROM `{$table_name}` WHERE `ID` > '{$last_imported_id}' ORDER BY `ID` ASC LIMIT {$posts_per_batch}";
  11.  
  12. // Esegui la query
  13. $posts_to_import = $wpdb->get_results($query);
  14.  
  15.  
  16. foreach ($posts_to_import as $post_data) {
  17.    
  18.     // Estrazione della data dall'ultimo segmento dell'URL
  19.     if (preg_match('/(\d{2})-(\d{2})-(\d{4})/', $post_data->Address, $matches)) {
  20.         $post_date = "{$matches[3]}-{$matches[2]}-{$matches[1]} 00:00:00";
  21.     } else {
  22.         // Tentativo di estrazione dell'anno e del mese dai segmenti precedenti
  23.         // Mappa i nomi dei mesi italiani ai loro corrispondenti numerici
  24.                 $mesi_italiani = array(
  25.                     'gennaio' => '01',
  26.                     'febbraio' => '02',
  27.                     'marzo' => '03',
  28.                     'aprile' => '04',
  29.                     'maggio' => '05',
  30.                     'giugno' => '06',
  31.                     'luglio' => '07',
  32.                     'agosto' => '08',
  33.                     'settembre' => '09',
  34.                     'ottobre' => '10',
  35.                     'novembre' => '11',
  36.                     'dicembre' => '12',
  37.                 );
  38.                
  39.                 // Esempio di utilizzo
  40.                 if (preg_match('#/archivio/(?:anno-)?(\d{4})/([A-Za-z]+)#i', $post_data->Address, $matches)) {
  41.                     $year = $matches[1];
  42.                     $monthName = strtolower($matches[2]);
  43.                     // Utilizza l'array associativo per convertire il nome del mese in numero
  44.                     $month = array_key_exists($monthName, $mesi_italiani) ? $mesi_italiani[$monthName] : '01';
  45.                     $post_date = "$year-$month-01 00:00:00";
  46.                 } else {
  47.                     $post_date = date('Y-m-d H:i:s'); // Fallback alla data corrente se non trova nulla
  48.                 }
  49.  
  50.     }
  51.  
  52.     $new_post = array(
  53.         'post_title'    => wp_strip_all_tags($post_data->{'h1 1'}),
  54.         'post_content'  => $post_data->{'contenuto 1'},
  55.         'post_status'   => 'publish',
  56.         'post_author'   => 1,
  57.         'post_type'     => 'post',
  58.         'post_date'     => $post_date
  59.     );
  60.  
  61.     $new_post_id = wp_insert_post($new_post);
  62.  
  63.     if ($new_post_id != 0) {
  64.         update_post_meta($new_post_id, 'old_url', $post_data->Address);
  65.         wp_set_post_terms($new_post_id, [15], 'category');
  66.             update_option('last_imported_id', $post_data->id);
  67.         // Ottieni il permalink del nuovo post
  68.         $new_permalink = get_permalink($new_post_id);
  69.    
  70.         // Preparazione della query di update
  71.         $wpdb->update(
  72.             $table_name,
  73.             array(
  74.                 'idnuova' => $new_post_id, // Nome della colonna da aggiornare con l'ID del nuovo post
  75.                 'urlnuova' => $new_permalink // Nome della colonna da aggiornare con il nuovo URL
  76.             ),
  77.             array('ID' => $post_data->id) // Condizione per l'update
  78.         );
  79.         }
  80.  
  81. }
  82.  
  83. echo "Importazione completata per $posts_per_batch post. Ultimo ID importato: " . get_option('last_imported_id');
  84. ?>
  85. <meta http-equiv="refresh" content="10">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement