php - Wordpress wp_insert_post is creating new Taxonomy term when creating new custom post - Stack Overflow

PHOTO EMBED

Sun Jun 04 2023 06:14:30 GMT+0000 (Coordinated Universal Time)

Saved by @leninzapata #php

   function save_function()
{

    $subject_term = 'subject';
    $my_subject_term = term_exists($subject_term, 'my_custom_taxonomy');   // check if term in website or no
    // Create Term if it doesn't exist
    if (!$my_subject_term) {
        $my_subject_term = wp_insert_term($subject_term, 'my_custom_taxonomy');
    }
    $custom_tax = array(
        'my_custom_taxonomy' => array(
            $my_subject_term['term_taxonomy_id'],
        )
    );

    // MESSAGE FIELDS
    $public_post = array(
        'post_title' => filter_input(INPUT_POST, 'title'),
        'post_author' => 1,
        'post_type' => 'message',
        'post_status' => 'pending',
        'tax_input' => $custom_tax
    );

    $post_id = wp_insert_post($public_post);

    
}
content_copyCOPY

https://stackoverflow.com/questions/64371962/wordpress-wp-insert-post-is-creating-new-taxonomy-term-when-creating-new-custom