Add Taxonomy Description with wp_set_post_terms

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 );
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x