function ctl_convert_existing_slugs() { global $wpdb; $posts = $wpdb->get_results("SELECT ID, post_name FROM {$wpdb->posts} WHERE post_name REGEXP('[^A-Za-z0-9\\-]+') AND post_status = 'publish'"); foreach ((array) $posts as $post) { $sanitized_name = ctl_sanitize_title(urldecode($post->post_name)); if ($post->post_name != $sanitized_name) { add_post_meta($post->ID, '_wp_old_slug', $post->post_name); $wpdb->update($wpdb->posts, array('post_name' => $sanitized_name), array('ID' => $post->ID)); } } $terms = $wpdb->get_results("SELECT term_id, slug FROM {$wpdb->terms} WHERE slug REGEXP('[^A-Za-z0-9\\-]+') "); foreach ((array) $terms as $term) { $sanitized_slug = ctl_sanitize_title(urldecode($term->slug)); if ($term->slug != $sanitized_slug) { $wpdb->update($wpdb->terms, array('slug' => $sanitized_slug), array('term_id' => $term->term_id)); } } }
function create_new_product($product) { $new_post = array('post_title' => $product['Product'], 'post_name' => ctl_sanitize_title($product['Product']), 'post_content' => $product['Long_description'], 'post_status' => 'publish', 'post_type' => 'product'); $skuu = $product['SKU']; $post_id = wp_insert_post($new_post); update_post_meta($post_id, '_sku', $skuu); update_post_meta($post_id, '_regular_price', (double) $product['Price']); update_post_meta($post_id, '_price', (double) $product['Price']); update_post_meta($post_id, '_manage_stock', true); update_post_meta($post_id, '_stock', $product['Qty']); //update_post_meta( $post_id, '_weight', $product['Weight'] ); update_post_meta($post_id, '_visibility', 'visible'); //wp_set_object_terms ($post_id, 'variable','product_type'); wp_set_object_terms($post_id, $product['cat_ids'][0], 'product_cat'); if ((int) $product['Qty'] > 0) { update_post_meta($post_id, '_stock_status', 'instock'); } //wp_set_object_terms($post_id, $product['cat_ids'], 'wpsc_product_category' ); // foreach ($product['cat_ids'] as $v){ // // } // $dir = dirname(__FILE__); // $imageFolder = $dir.'/../import/'; // $imageFile = $product['ID'].'.jpg'; // $imageFull = $imageFolder.$imageFile; // only need these if performing outside of admin environment require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; // example image //$image = 'http://localhost/wordpress/wp-content/import/'.$product['ID'].'.jpg'; // magic sideload image returns an HTML image, not an ID $media = media_sideload_image($product['img_path'], $post_id); // therefore we must find it so we can set it as featured ID if (!empty($media) && !is_wp_error($media)) { $args = array('post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'any', 'post_parent' => $post_id); // reference new image to set as featured $attachments = get_posts($args); if (isset($attachments) && is_array($attachments)) { foreach ($attachments as $attachment) { // grab source of full size images (so no 300x150 nonsense in path) $image = wp_get_attachment_image_src($attachment->ID, 'full'); // determine if in the $media image we created, the string of the URL exists if (strpos($media, $image[0]) !== false) { // if so, we found our image. set it as thumbnail set_post_thumbnail($post_id, $attachment->ID); // only want one image break; } } } } }