2017-07-09 7 views
0

woocommerceプロダクト用に1つのバリエーションを作成する必要がありますが、保存するたびに別のバリエーションが作成されます。 私はそれを保存ヒットするたびに別のバリエーションを作成するなぜのfunctions.phpwoocommerceプロダクト用の単一のバリエーションを作成

からループの外で働いて、ちょうど1バリエーション/子を作成するために助けが必要

add_action (save_post, create_new_vars); 
function create_new_vars ($post) { 
global $post; 
$children = get_pages('child_of='.$post->ID); 
if(count($children) >= 1) {  
return; 
} 

$my_post = array(
    'post_title' => 'Color for #' . $post->ID, 
    'post_name' => get_the_title($post->ID), 
    'post_status' => 'publish', 
    'post_parent' => $post->ID, 
    'post_type' => 'product_variation', 
    'guid' => home_url() . '/?product_variation=' . get_the_title($post->ID) 
); 

    remove_action('save_post', __FUNCTION__); 
    $attID = wp_insert_post($my_post); 

} 

答えて

0

を投稿OK答えは

です
add_action (save_post, create_new_vars); 
function create_new_vars ($post) { 
global $post; 
$args = array(
    'post_title' => 'Color for #' . $post->ID, 
    'post_name' => get_the_title($post->ID), 
    'post_status' => 'publish', 
    'post_parent' => $post->ID, 
    'post_type' => 'product_variation', 
    'guid' => home_url() . '/?product_variation=' . get_the_title($post->ID)  
); 
$children = get_children($args); 
if(count($children) < 1) {  


$my_post = array(
    'post_title' => 'Color for #' . $post->ID, 
    'post_name' => get_the_title($post->ID), 
    'post_status' => 'publish', 
    'post_parent' => $post->ID, 
    'post_type' => 'product_variation', 
    'guid' => home_url() . '/?product_variation=' . get_the_title($post->ID) 
); 

    remove_action('save_post', __FUNCTION__); 
    $attID = wp_insert_post($my_post); 

} 
} 
関連する問題