WordPress – How to auto insert some words from title to tags

I want to auto-insert words in title of a post and begin by # in tags. and use this function. but when i publish a post, all words inserted.

function convert_to_tag( $post_id ) {
    $post       = get_post( $post_id );
    $tax        = isset( $types[$post->post_type] ) ? $types[$post->post_type] : 'post_tag';
    if( isset( $post->post_title ) ) :
        $title_words    = explode( ' ', $post->post_title );
        foreach( $title_words as $word ) :
            if (0 == strpos($word, '#')) :
                wp_insert_term(
                    $word,
                    $tax,
                    array()
                );
            endif;
        endforeach;
    endif;
}
add_action( 'save_post', 'convert_to_tag' );

Read more here: WordPress – How to auto insert some words from title to tags

Leave a Reply

Your email address will not be published. Required fields are marked *