Assign Multiple Values from Post Fields to Taxonomy Terms

Question

I am working on a listings site where I need to take the post data of multiple fields and assign them as taxonomy terms. The site is built using WordPress and Toolset. I have this working for one field but am having trouble adding multiple terms. All of the fields are assigning a US State value. All of the State terms are already created. The first one is pulling from an address field and the others select lists. Each of the fields works if I do just one. But I can’t figure out how to combine them into an array that assigns them all. Creating multiple functions does not work as each function seems to overwrite the previous value instead of adding the new ones.

This the code that works for one field:

// add state to category
add_action( 'save_post', 'ied_state_save_post', 10,3 );
function ied_state_save_post( $post_id, $post, $update ) {
    // exit if not the target post type
    if ( ('listing' !== $post->post_type) OR ($post->post_status == 'auto-draft') OR (empty($_POST)) ) {
        return;
    }
    // current post ID
    $post_id = $post->ID;
    // target taxonomy slug
    $taxonomy_slug = 'state';
    // $_POST['wpcf-state'] value exists, it means that post is being added using the front-end form
    if(!empty($_POST['wpcf-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    // $_POST['wpcf']['state'] value exists, it means that post is being added using the back-end post edit screen
    if(!empty($_POST['wpcf']['state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
}
  
// function that processes the taxonomy attachment
function ied_new_state_term($post_id, $term_name, $taxonomy_slug) {
    // check if the term already exists
    $term = term_exists( $term_name, $taxonomy_slug );
    // if does exist
    if ( $term !== 0 && $term !== null ) {
        // attach it to the current post
        wp_set_post_terms( $post_id, $term['term_id'], $taxonomy_slug );
    }
    // if doesn't exist
    else
    {   
        // first add it as a new term in the taxonomy
        $inset_new_term = wp_insert_term($term_name, $taxonomy_slug);
        if ( ! is_wp_error( $inset_new_term ) )
        {
            // and then attach this new term to the current post
            $term_id = $inset_new_term['term_id'];
            wp_set_post_terms( $post_id, $term_id, $taxonomy_slug );
        }
    }
}

These are the fields I want to add to it:

$_POST['wpcf-license-state']
$_POST['wpcf-2nd-license-state']
$_POST['wpcf-3rd-license-state']
$_POST['wpcf-4th-license-state']
$_POST['wpcf-5th-license-state']
$_POST['wpcf-6th-license-state']
$_POST['wpcf-7th-license-state']
$_POST['wpcf-8th-license-state']
$_POST['wpcf-9th-license-state']
$_POST['wpcf-10th-license-state']

I asked this in Toolset support first and they provided the following code but it only saves the last value you enter as a term, not all of them:

add_action( 'save_post', 'ied_state_save_post', 10,3 );
function ied_state_save_post( $post_id, $post, $update ) {
    // exit if not the target post type
    if ( ('listing' !== $post->post_type) OR ($post->post_status == 'auto-draft') OR (empty($_POST)) ) {
        return;
    }
    // current post ID
    $post_id = $post->ID;
    // target taxonomy slug
    $taxonomy_slug = 'state';
    // $_POST['wpcf-state'] value exists, it means that post is being added using the front-end form
    if(!empty($_POST['wpcf-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    // $_POST['wpcf']['state'] value exists, it means that post is being added using the back-end post edit screen
    if(!empty($_POST['wpcf']['state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
    //// $_POST['wpcf-license-state']
    if(!empty($_POST['wpcf-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
    //// $_POST['wpcf-2nd-license-state']
    if(!empty($_POST['wpcf-2nd-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-2nd-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['2nd-license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['2nd-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
    //// $_POST['wpcf-3rd-license-state']
    if(!empty($_POST['wpcf-3rd-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-3rd-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['3rd-license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['3rd-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
    //// $_POST['wpcf-4th-license-state']
    if(!empty($_POST['wpcf-4th-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-4th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['4th-license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['4th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
    //// $_POST['wpcf-5th-license-state']
    if(!empty($_POST['wpcf-5th-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-5th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['5th-license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['5th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
     
    //// $_POST['wpcf-6th-license-state']
    if(!empty($_POST['wpcf-6th-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-6th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['6th-license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['6th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
    //// $_POST['wpcf-7th-license-state']
    if(!empty($_POST['wpcf-7th-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-7th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['7th-license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['7th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
    //// $_POST['wpcf-8th-license-state']
    if(!empty($_POST['wpcf-8th-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-8th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['8th-license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['8th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
    //// $_POST['wpcf-9th-license-state']
    if(!empty($_POST['wpcf-9th-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-9th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['9th-license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['9th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
    //// $_POST['wpcf-10th-license-state']
    if(!empty($_POST['wpcf-10th-license-state'])) {
        // get the term's name from the form's state field
        $term_name = $_POST['wpcf-10th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
    if(!empty($_POST['wpcf']['10th-license-state'])) {
        // get the term's name from the state field
        $term_name = $_POST['wpcf']['10th-license-state'];
        // call the function that processes the taxonomy attachment
        ied_new_state_term($post_id, $term_name, $taxonomy_slug);
    }
     
     
}
   
// function that processes the taxonomy attachment
function ied_new_state_term($post_id, $term_name, $taxonomy_slug) {
    // check if the term already exists
    $term = term_exists( $term_name, $taxonomy_slug );
    // if does exist
    if ( $term !== 0 && $term !== null ) {
        // attach it to the current post
        wp_set_post_terms( $post_id, $term['term_id'], $taxonomy_slug );
    }
    // if doesn't exist
    else
    {   
        // first add it as a new term in the taxonomy
        $inset_new_term = wp_insert_term($term_name, $taxonomy_slug);
        if ( ! is_wp_error( $inset_new_term ) )
        {
            // and then attach this new term to the current post
            $term_id = $inset_new_term['term_id'];
            wp_set_post_terms( $post_id, $term_id, $taxonomy_slug );
        }
    }
}

It seems like the above needs to be in a foreach loop? I was following this post WP_insert_term how to insert multiple values as taxonomny term? but the code I came up with created a new blank term instead of assigning multiple terms. I think it was trying to combine all the terms into one instead of an array.

0
jenniferannwalsh 6 months 2022-02-25T21:44:32-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse