add_term_meta does not execute

Asked

Viewed 26 times

1

I need to create a term with the same name of the post, for this I’m using the action save_post. The term is normally created but when adding metadata the add_term_meta function does not perform. 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 executes outside the create_product_group function.

Where am I going wrong?

1 answer

2


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

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

get_term_by( 'id', $product_group_id['term_id'], 'product_group' );
  • Exact Ricardo, thanks for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.