add_term_meta does not execute

1

I need to create a term with the same name as the post, so I'm using the action save_post. The term is normally created but when adding metadata the add_term_meta function does not execute. This is the code.

function create_product_group($post_id){
  if(get_post_type($post_id) != 'product'){
    return;
  }

  $product_group = get_term_by( 'name', get_the_title($post_id) , 'product_group');

  if(!$product_group){
    $product_group_id = wp_insert_term( get_the_title($post_id) , 'product_group', array('description' => get_the_excerpt( $post_id )));

    add_term_meta($product_group_id,"book_group_author" , 'Elder Carvalho'); //adiciona autores
    add_term_meta($product_group_id,"book_group_image", 'http://teste.jpg'); //imagem do livro
    add_term_meta($product_group_id,"book_group_category", 'geral'); //categorias do livro

    $product_group = get_term_by('id', $product_group_id, 'product_group');
  }

  wp_set_object_terms( $post_id, $product_group->term_id , 'product_group');
}
add_action('save_post','create_product_group');

add_term_meta normally runs outside the create_product_group function.

Where am I going wrong?

    
asked by anonymous 01.04.2017 / 22:46

1 answer

2

wp_insert_term() returns a array containing term_id and term_taxonomy_id , when executed correctly , then you must pass term_id to the add_term_meta and get_term_by functions:

add_term_meta( $product_group_id['term_id'], "book_group_author" , 'Elder Carvalho' );

get_term_by( 'id', $product_group_id['term_id'], 'product_group' );
    
02.04.2017 / 03:12