Ver Mensaje Individual
  #6 (permalink)  
Antiguo 15/04/2011, 09:36
Avatar de AlZuwaga
AlZuwaga
Colaborador
 
Fecha de Ingreso: febrero-2001
Ubicación: 34.517 S, 58.500 O
Mensajes: 14.550
Antigüedad: 23 años, 2 meses
Puntos: 535
Respuesta: Importar BD personalizada (sus sugerencias necesito)

Cita:
Por cierto, yo me hubiera evitado lo del ID original como custom field, haciendo que sea el ID del post en WP. La ultima vez hice los INSERTs al wp_posts a mano (saltandome el api) para que quedaran :P Digo, si es que son numéricos.
Bueno, finalmente me decidí por una mezcla: Hice uso del API con wp_insert_post() y add_post_meta() (para no escribir tanto código), pero luego tocó hacer una serie de UPDATES (también con el API):

- a wp_posts para modificar el valor del ID (de ID generado por WP a ID de origen)
- a wp_postmeta para asociar los custom fields a este ID de origen
- a wp_term_relationships para asociar los tags y las categorías a este ID de origen

El código que usé es éste:

Código PHP:
Ver original
  1. // me conecto a la "tabla fuente"
  2. $datos = $wpdb->get_results("SELECT * FROM noticias LIMIT 0, 1000");
  3.  
  4.  
  5. foreach ($datos as $dato) {
  6.     // previamente creé las categorías en WordPress. lógicamente no tienen los mismos ID's, así que tocó hacer una "traducción"de ellos
  7.     // if's concatenados usando una hoja de cálculo de excel... sé que era mejor un switch, pero esto fue más rápido :)
  8.     if($dato->ID_LINK == 1) {$nuevo_id_cat = 3;}
  9.     if($dato->ID_LINK == 38) {$nuevo_id_cat = 4;}
  10.     if($dato->ID_LINK == 2) {$nuevo_id_cat = 5;}
  11.     if($dato->ID_LINK == 3) {$nuevo_id_cat = 6;}
  12.     if($dato->ID_LINK == 4) {$nuevo_id_cat = 7;}
  13.     if($dato->ID_LINK == 5) {$nuevo_id_cat = 8;}
  14.     if($dato->ID_LINK == 47) {$nuevo_id_cat = 9;}
  15.     if($dato->ID_LINK == 43) {$nuevo_id_cat = 10;}
  16.     if($dato->ID_LINK == 59) {$nuevo_id_cat = 11;}
  17.     if($dato->ID_LINK == 7) {$nuevo_id_cat = 12;}
  18.     if($dato->ID_LINK == 6) {$nuevo_id_cat = 13;}
  19.     if($dato->ID_LINK == 49) {$nuevo_id_cat = 14;}
  20.     if($dato->ID_LINK == 50) {$nuevo_id_cat = 15;}
  21.     if($dato->ID_LINK == 56) {$nuevo_id_cat = 16;}
  22.     if($dato->ID_LINK == 61) {$nuevo_id_cat = 17;}
  23.     if($dato->ID_LINK == 24) {$nuevo_id_cat = 18;}
  24.     if($dato->ID_LINK == 8) {$nuevo_id_cat = 19;}
  25.     if($dato->ID_LINK == 34) {$nuevo_id_cat = 20;}
  26.     if($dato->ID_LINK == 35) {$nuevo_id_cat = 21;}
  27.     if($dato->ID_LINK == 9) {$nuevo_id_cat = 22;}
  28.     if($dato->ID_LINK == 10) {$nuevo_id_cat = 23;}
  29.     if($dato->ID_LINK == 11) {$nuevo_id_cat = 24;}
  30.     if($dato->ID_LINK == 13) {$nuevo_id_cat = 25;}
  31.     if($dato->ID_LINK == 26) {$nuevo_id_cat = 26;}
  32.     if($dato->ID_LINK == 14) {$nuevo_id_cat = 27;}
  33.     if($dato->ID_LINK == 46) {$nuevo_id_cat = 28;}
  34.     if($dato->ID_LINK == 15) {$nuevo_id_cat = 29;}
  35.     if($dato->ID_LINK == 25) {$nuevo_id_cat = 30;}
  36.     if($dato->ID_LINK == 60) {$nuevo_id_cat = 31;}
  37.     if($dato->ID_LINK == 54) {$nuevo_id_cat = 32;}
  38.     if($dato->ID_LINK == 21) {$nuevo_id_cat = 33;}
  39.     if($dato->ID_LINK == 22) {$nuevo_id_cat = 34;}
  40.     if($dato->ID_LINK == 18) {$nuevo_id_cat = 35;}
  41.     if($dato->ID_LINK == 17) {$nuevo_id_cat = 36;}
  42.     if($dato->ID_LINK == 19) {$nuevo_id_cat = 37;}
  43.     if($dato->ID_LINK == 23) {$nuevo_id_cat = 38;}
  44.     if($dato->ID_LINK == 52) {$nuevo_id_cat = 39;}
  45.     if($dato->ID_LINK == 44) {$nuevo_id_cat = 40;}
  46.     if($dato->ID_LINK == 53) {$nuevo_id_cat = 41;}
  47.     if($dato->ID_LINK == 20) {$nuevo_id_cat = 42;}
  48.  
  49.     //defino los datos a guardar en el post
  50.     $post = array(
  51.     //  'ID' => [ <post id> ] //Are you updating an existing post?
  52.     //  'menu_order' => [ <order> ] //If new post is a page, sets the order should it appear in the tabs.
  53.       'comment_status' => 'closed', // 'closed' means no comments.
  54.       'ping_status' => 'closed', // 'closed' means pingbacks or trackbacks turned off
  55.       'pinged' => '', //?
  56.       'post_author' => 1, //The user ID number of the author.
  57.       'post_category' => array($nuevo_id_cat), //Add some categories.
  58.       'post_content' => trim($dato->NOTICIA), //The full text of the post.
  59.       'post_date' => trim($dato->FECHA),//[ Y-m-d H:i:s ] //The time post was made.
  60.       'post_date_gmt' => trim($dato->FECHA), //[ Y-m-d H:i:s ] //The time post was made, in GMT.
  61.       'post_excerpt' => trim($dato->COPETE), //For all your post excerpt needs.
  62.       'post_name' => trim($dato->TITULO), // The name (slug) for your post
  63.       'post_parent' => 0, //Sets the parent of the new post.
  64.       'post_password' => '', //password for post?
  65.       'post_status' => 'publish', //[ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //Set the status of the new post.
  66.       'post_title' => trim($dato->TITULO), //The title of your post.
  67.       'post_type' => 'post', //[ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //You may want to insert a regular post, page, link, a menu item or some custom post type
  68.       'tags_input' => trim($dato->CIUDAD), //For tags.
  69.       'to_ping' => '', //?
  70.     );
  71.    
  72.     //tomo el id del registro original
  73.     $idOriginal = $dato->ID_NOTICIAS;
  74.    
  75.     //inserto el contenido del post obteniendo el id generador por WP
  76.     $new_post_id = wp_insert_post( $post );
  77.  
  78.     //ELIMINAR DE VERSION DEFINITIVA: genero un custom field con el id original
  79.     add_post_meta($new_post_id, 'id_noticias', $dato->ID_NOTICIAS);
  80.  
  81.     //ELIMINAR DE VERSION DEFINITIVA: genero un custom field con el dato de la ciudad
  82.     if(trim($dato->CIUDAD) !== ''){
  83.         add_post_meta($new_post_id, 'ciudad', trim($dato->CIUDAD));
  84.     }
  85.  
  86.     //DECIDIR QUE HAGO CON ESTO EN VERSION DEFINITIVA: genero un custom field con el dato de la firma
  87.     if(trim($dato->FIRMA) !== ''){
  88.         add_post_meta($new_post_id, 'firma', trim($dato->FIRMA));
  89.     }
  90.  
  91.     //DECIDIR QUE HAGO CON ESTO EN VERSION DEFINITIVA: genero un custom field con el dato de la fuente
  92.     if(trim($dato->FUENTE) !== ''){
  93.         add_post_meta($new_post_id, 'fuente', trim($dato->FUENTE));
  94.     }
  95.  
  96.     //DECIDIR QUE HAGO CON ESTO EN VERSION DEFINITIVA: genero un custom field con el dato de la foto
  97.     if(trim($dato->FOTO) !== ''){
  98.         add_post_meta($new_post_id, 'foto_original', $dato->FOTO);
  99.     }
  100.  
  101.     //actualizo el ID del post recién insertado con el ID original
  102.     $wpdb->update( 'wp_posts', array( 'ID' => $idOriginal), array( 'ID' => $new_post_id ), $format = null, $where_format = null );
  103.     //actualizo los custom field antes insertados para asociarlos al ID original
  104.     $wpdb->update( 'wp_postmeta', array( 'post_id' => $idOriginal), array( 'post_id' => $new_post_id ), $format = null, $where_format = null );
  105.     //actualizo los tags y las categorías que tenga el post recién insertado para asociarlos al ID original
  106.     $wpdb->update( 'wp_term_relationships', array( 'object_id' => $idOriginal), array( 'object_id' => $new_post_id ), $format = null, $where_format = null );
  107.  
  108. } //foreach

Así como está... Le ves algo que haya omitido, Javier? (por ejemplo hacer un update a alguna tabla que no me haya percatado).

Gracias!
__________________
...___...