Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('wp-load.php');
- $posts_per_batch = 1000;
- $last_imported_id = get_option('last_imported_id', 0);
- global $wpdb;
- $table_name = 'screaming_export';
- $query = "SELECT * FROM `{$table_name}` WHERE `ID` > '{$last_imported_id}' ORDER BY `ID` ASC LIMIT {$posts_per_batch}";
- // Esegui la query
- $posts_to_import = $wpdb->get_results($query);
- foreach ($posts_to_import as $post_data) {
- // Estrazione della data dall'ultimo segmento dell'URL
- if (preg_match('/(\d{2})-(\d{2})-(\d{4})/', $post_data->Address, $matches)) {
- $post_date = "{$matches[3]}-{$matches[2]}-{$matches[1]} 00:00:00";
- } else {
- // Tentativo di estrazione dell'anno e del mese dai segmenti precedenti
- // Mappa i nomi dei mesi italiani ai loro corrispondenti numerici
- $mesi_italiani = array(
- 'gennaio' => '01',
- 'febbraio' => '02',
- 'marzo' => '03',
- 'aprile' => '04',
- 'maggio' => '05',
- 'giugno' => '06',
- 'luglio' => '07',
- 'agosto' => '08',
- 'settembre' => '09',
- 'ottobre' => '10',
- 'novembre' => '11',
- 'dicembre' => '12',
- );
- // Esempio di utilizzo
- if (preg_match('#/archivio/(?:anno-)?(\d{4})/([A-Za-z]+)#i', $post_data->Address, $matches)) {
- $year = $matches[1];
- $monthName = strtolower($matches[2]);
- // Utilizza l'array associativo per convertire il nome del mese in numero
- $month = array_key_exists($monthName, $mesi_italiani) ? $mesi_italiani[$monthName] : '01';
- $post_date = "$year-$month-01 00:00:00";
- } else {
- $post_date = date('Y-m-d H:i:s'); // Fallback alla data corrente se non trova nulla
- }
- }
- $new_post = array(
- 'post_title' => wp_strip_all_tags($post_data->{'h1 1'}),
- 'post_content' => $post_data->{'contenuto 1'},
- 'post_status' => 'publish',
- 'post_author' => 1,
- 'post_type' => 'post',
- 'post_date' => $post_date
- );
- $new_post_id = wp_insert_post($new_post);
- if ($new_post_id != 0) {
- update_post_meta($new_post_id, 'old_url', $post_data->Address);
- wp_set_post_terms($new_post_id, [15], 'category');
- update_option('last_imported_id', $post_data->id);
- // Ottieni il permalink del nuovo post
- $new_permalink = get_permalink($new_post_id);
- // Preparazione della query di update
- $wpdb->update(
- $table_name,
- array(
- 'idnuova' => $new_post_id, // Nome della colonna da aggiornare con l'ID del nuovo post
- 'urlnuova' => $new_permalink // Nome della colonna da aggiornare con il nuovo URL
- ),
- array('ID' => $post_data->id) // Condizione per l'update
- );
- }
- }
- echo "Importazione completata per $posts_per_batch post. Ultimo ID importato: " . get_option('last_imported_id');
- ?>
- <meta http-equiv="refresh" content="10">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement