admin管理员组

文章数量:1326315

I am adding tags during wp_insert_post using wp_set_post_terms. But I have no clue how to add a tag description with my code. I have not found anything out there.

I am adding tags during wp_insert_post using wp_set_post_terms. But I have no clue how to add a tag description with my code. I have not found anything out there.

Share Improve this question asked Aug 9, 2020 at 8:55 Robin AlexanderRobin Alexander 1538 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You can't use wp_set_post_terms() to set the term description, but you can use wp_insert_term() to create the term and set the description (plus other details), and then pass the term ID to wp_set_post_terms().

Working example for one term:

$taxonomy = 'category';
$term_name = 'Foo Bar';

// First we create the term (if it doesn't already exist).
if ( ! $term = term_exists( $term_name, $taxonomy ) ) {
    $term = wp_insert_term( $term_name, $taxonomy, array(
        'description' => 'Test category',
    ) );
}

// Now assign the term to the post (here the post ID is 1).
if ( $term && ! is_wp_error( $term ) ) {
    wp_set_post_terms( 1, $term['term_id'], $taxonomy );
}

本文标签: termsAdd Taxonomy Description with wpsetpostterms