Created
May 31, 2012 17:40
-
-
Save ganicus/2844975 to your computer and use it in GitHub Desktop.
Wordpress: Multiple Post Upload File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Template Name: MaxDebug | |
| */ | |
| // Get WP | |
| include_once"wp-config.php"; | |
| include_once"wp-load.php"; | |
| include_once"wp-includes/wp-db.php"; | |
| //Connect To Old Database | |
| $oldbros = new wpdb('root','Catalin@2040','sqlbros','localhost'); | |
| //first get the term (I used slug, but you can aslo use 'name'), see: http://codex.wordpress.org/Function_Reference/get_term_by | |
| $term = get_term_by( 'slug', 'xeriscape-landscape-design', 'scope' ); | |
| //then get the term_id | |
| $term_id = $term->term_id; | |
| //Get Old DB Info | |
| $ob_tree = $oldbros->get_results('SELECT t1.name, t1.shortDescription, t2.color, t2.price, t2.thickness, t2.coverage, t2.type, t2.quantity, t2.size, t3.filename FROM ProductsTree AS t1 LEFT OUTER JOIN bourgetbros_catagory_attributes AS t2 ON t2.cat_id = t1.idProductsTree LEFT OUTER JOIN Image AS t3 ON t3.idContent = t1.idProductsTree WHERE t3.idContent=774'); | |
| foreach ($ob_tree as $tree) { | |
| $tree_item = array( | |
| 'post_title' => $tree->name, | |
| 'post_content' => $tree->shortDescription, | |
| 'post_status' => 'publish', | |
| 'post_author' => 1, | |
| 'post_type' => 'idea', | |
| 'tax_input' => array( 'scope' => $term_id ) | |
| ); | |
| //Get Meta Values | |
| $ob_color = $tree->color; | |
| $ob_price = $tree->price; | |
| $ob_coverage = $tree->coverage; | |
| $ob_type = $tree->type; | |
| $ob_qty = $tree->quantity; | |
| $ob_size = $tree->size; | |
| $ob_thickness = $tree->thickness; | |
| //Insert Post and Return Post ID | |
| $post_id = wp_insert_post( $tree_item ); | |
| //update post meta | |
| add_post_meta($post_id, 'bb_product_color', $ob_color, true); | |
| add_post_meta($post_id, 'bb_product_size', $ob_size, true); | |
| add_post_meta($post_id, 'bb_product_type', $ob_type, true); | |
| add_post_meta($post_id, 'bb_product_coverage', $ob_coverage, true); | |
| add_post_meta($post_id, 'bb_product_qty', $ob_qty, true); | |
| add_post_meta($post_id, 'bb_products_thickness', $ob_thickness, true); | |
| $post_meta_id = add_post_meta($post_id, 'bb_product_price', $ob_price, true); | |
| //add-hyphpen to dtabase name to match actual filename | |
| if (isset($tree->filename)) { | |
| $file = str_replace( " ", "-", $tree->filename); | |
| //update feautred image | |
| $filename = ABSPATH . 'wp-content/uploads/2012/02/' . $file; | |
| $wp_filetype = wp_check_filetype(basename($filename), null ); | |
| $attachment = array( | |
| 'post_mime_type' => $wp_filetype['type'], | |
| 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), | |
| 'post_content' => '', | |
| 'post_status' => 'inherit' | |
| ); | |
| $attach_id = wp_insert_attachment( $attachment, $filename, $post_id ); | |
| // you must first include the image.php file | |
| // for the function wp_generate_attachment_metadata() to work | |
| require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
| $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
| wp_update_attachment_metadata( $attach_id, $attach_data ); | |
| update_post_meta($post_id, '_thumbnail_id', $attach_id); | |
| // set as featured image | |
| } | |
| } | |
| echo "Done"; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment