Skip to content

Instantly share code, notes, and snippets.

@trangsihung
Created August 8, 2016 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trangsihung/f9507f65053c53ea47e0ec7730a02710 to your computer and use it in GitHub Desktop.
Save trangsihung/f9507f65053c53ea47e0ec7730a02710 to your computer and use it in GitHub Desktop.
<?php
function _split_product_size() {
$term_need_delete = array();
$q = new WP_Query(array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'value' => 'pa_size',
'compare' => 'like'
),
array(
'key' => '_solid_checked',
'compare' => 'NOT EXISTS'
),
),
));
if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
$id = get_the_id();
$old_size = array();
# Get current terms
$_temp = wp_get_object_terms($id, 'pa_size', 'all');
// var_dump($_temp);
foreach ($_temp as $term) {
$old_size_id = $term->term_id;
$term_need_delete[] = $term->term_id;
$old_size_name = trim( str_replace(' ', '', $term->name) );
$new_terms = explode(',', $old_size_name);
$insert_terms = array();
foreach ($new_terms as $value) {
$term_id = wp_insert_term($value, 'pa_size');
if ( is_wp_error( $term_id ) ) {
$_t = get_term_by('name', $value, 'pa_size');
$term_id = $_t->term_id;
}
$insert_terms[] = $term_id;
}
wp_remove_object_terms($id, $old_size_id, 'pa_size');
wp_set_object_terms( $id, $insert_terms, 'pa_size', true );
add_post_meta( $id, '_solid_checked', 1, true );
}
endwhile; wp_reset_query(); endif;
}
add_action('admin_init', '_split_product_size');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment