Guest User

Untitled

a guest
May 1st, 2019
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. add_action( 'init', function() {
  3.     if ( 'migrate' !== filter_input( INPUT_GET, 'action' ) ) {
  4.         return;
  5.     }
  6.  
  7.     $query = new WP_Query( [
  8.         'posts_per_page' => -1,
  9.         'post_type'      => 'post',
  10.         'post_status'    => 'any',
  11.     ] );
  12.     if ( ! $query->have_posts() ) {
  13.         return;
  14.     }
  15.  
  16.     global $wpdb;
  17.     $table_name = 'your_table';
  18.  
  19.     while ( $query->have_posts() ) {
  20.         $query->the_post();
  21.  
  22.         $field_id_1 = 'image_26';
  23.         $field_value_1 = get_post_meta( get_the_ID(), $field_id_1, false ); // Image field store multiple values.
  24.  
  25.         $field_id_2 = 'text_1';
  26.         $field_value_2 = get_post_meta( get_the_ID(), $field_id_2, true ); // Text field store a single value.
  27.  
  28.         $data = [
  29.             'ID'      => get_the_ID(),
  30.             $field_id_1 => serialize( $field_value_1 ),
  31.             $field_id_2 => $field_value_2,
  32.         ];
  33.         $wpdb->delete( $table_name, ['ID' => get_the_ID()] ); // Delete existing data.
  34.         $wpdb->insert( $table_name, $data ); // Insert data to the table.
  35.     }
  36. } );
RAW Paste Data