• 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 ?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    What do you mean by “inserting Post Tags 2 times”?
    wp_insert_term() will return an error if you try to insert a term that already exists.

Viewing 1 replies (of 1 total)
  • The topic ‘Insert Tag in WordPress Post’ is closed to new replies.