admin管理员组文章数量:1122832
I have below code.
$args = array(
'post_title' => $this->h1,
'post_content' => $this->profile_text,
'post_status' => 'publish',
'post_type' => 'post',
'meta_input' => $this->to_meta_array(),
);
$post_id = wp_insert_post( $args );
if ( ! is_wp_error( $post_id ) ) {
$this->id = $post_id;
$success = true;
}
$united_states_term = term_exists('United States', 'category');
if (!$united_states_term) {
$united_states_term = wp_insert_term('United States', 'category');
}
$united_states_term_id = $united_states_term['term_id'] ?? null;
if ($united_states_term_id) {
wp_set_post_categories($this->id, [$united_states_term_id], true);
}
$uncategorized_term_id = get_option('default_category');
$current_categories = wp_get_post_categories($this->id);
if (($key = array_search($uncategorized_term_id, $current_categories)) !== false) {
unset($current_categories[$key]);
}
wp_set_post_categories($this->id, $current_categories);
// added tags.
if ( 0 !== $this->id ) {
// get country category.
$country_term = get_term_by( 'slug', 'country', 'category' );
if ( $country_term ) {
$country_term_id = $country_term->term_id;
} else {
$country_term = wp_insert_term( 'Country', 'category', array( 'slug' => 'country' ) );
$country_term_id = $country_term['term_id'];
}
$tags = array();
$categories = array();
foreach ( $this->tags as $tag ) {
// insert new term.
if ( $tag['is_country'] ) {
// check exists tag.
$term = get_term_by( 'slug', $tag['slug'], 'category' );
// add country category.
$categories[] = $country_term_id;
if ( $term ) {
$categories[] = $term->term_id;
continue;
}
$new_term = wp_insert_term(
$tag['name'],
'category',
array(
'slug' => $tag['slug'],
'parent' => $country_term_id,
)
);
if ( ! is_wp_error( $new_term ) ) {
$categories[] = $new_term['term_id'];
}
} else {
// check exists tag.
$term = get_term_by( 'slug', $tag['slug'], 'post_tag' );
if ( $term ) {
$tags[] = $tag['slug'];
continue;
}
$new_term = wp_insert_term( $tag['name'], 'post_tag', array( 'slug' => $tag['slug'] ) );
if ( ! is_wp_error( $new_term ) ) {
$tags[] = $tag['slug'];
}
}
}
// unique value in array.
$tags = array_unique( $tags );
$categories = array_unique( $categories );
if ( ! empty( $tags ) ) {
wp_set_post_tags( $this->id, $tags, false );
}
if ( ! empty( $categories ) ) {
wp_set_post_categories( $this->id, $categories, false );
}
}
This code is inserting Post Tags 2 times.
How can I insert Tag only once ?
I have below code.
$args = array(
'post_title' => $this->h1,
'post_content' => $this->profile_text,
'post_status' => 'publish',
'post_type' => 'post',
'meta_input' => $this->to_meta_array(),
);
$post_id = wp_insert_post( $args );
if ( ! is_wp_error( $post_id ) ) {
$this->id = $post_id;
$success = true;
}
$united_states_term = term_exists('United States', 'category');
if (!$united_states_term) {
$united_states_term = wp_insert_term('United States', 'category');
}
$united_states_term_id = $united_states_term['term_id'] ?? null;
if ($united_states_term_id) {
wp_set_post_categories($this->id, [$united_states_term_id], true);
}
$uncategorized_term_id = get_option('default_category');
$current_categories = wp_get_post_categories($this->id);
if (($key = array_search($uncategorized_term_id, $current_categories)) !== false) {
unset($current_categories[$key]);
}
wp_set_post_categories($this->id, $current_categories);
// added tags.
if ( 0 !== $this->id ) {
// get country category.
$country_term = get_term_by( 'slug', 'country', 'category' );
if ( $country_term ) {
$country_term_id = $country_term->term_id;
} else {
$country_term = wp_insert_term( 'Country', 'category', array( 'slug' => 'country' ) );
$country_term_id = $country_term['term_id'];
}
$tags = array();
$categories = array();
foreach ( $this->tags as $tag ) {
// insert new term.
if ( $tag['is_country'] ) {
// check exists tag.
$term = get_term_by( 'slug', $tag['slug'], 'category' );
// add country category.
$categories[] = $country_term_id;
if ( $term ) {
$categories[] = $term->term_id;
continue;
}
$new_term = wp_insert_term(
$tag['name'],
'category',
array(
'slug' => $tag['slug'],
'parent' => $country_term_id,
)
);
if ( ! is_wp_error( $new_term ) ) {
$categories[] = $new_term['term_id'];
}
} else {
// check exists tag.
$term = get_term_by( 'slug', $tag['slug'], 'post_tag' );
if ( $term ) {
$tags[] = $tag['slug'];
continue;
}
$new_term = wp_insert_term( $tag['name'], 'post_tag', array( 'slug' => $tag['slug'] ) );
if ( ! is_wp_error( $new_term ) ) {
$tags[] = $tag['slug'];
}
}
}
// unique value in array.
$tags = array_unique( $tags );
$categories = array_unique( $categories );
if ( ! empty( $tags ) ) {
wp_set_post_tags( $this->id, $tags, false );
}
if ( ! empty( $categories ) ) {
wp_set_post_categories( $this->id, $categories, false );
}
}
This code is inserting Post Tags 2 times.
How can I insert Tag only once ?
Share Improve this question edited Apr 2, 2024 at 6:41 Foysal asked Apr 2, 2024 at 6:07 FoysalFoysal 4451 gold badge5 silver badges15 bronze badges1 Answer
Reset to default 0If you are using wp_set_post_tags
this you can pass either
array of exiting tags id or array of string.
If it is a string then it will create a new one and assign that, if you are passing an id then it will assign that term_id of post_tag.
As per your code I can see you are passing tag's slug and this is being treated as a string and that is why it is creating the tag again.
本文标签: Insert Tag in WordPress Post
版权声明:本文标题:Insert Tag in WordPress Post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312011a1934946.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论