Change the term based on the value of a $variable using wp_update_post in submitting a form

Background

I am using a fronted form to submit data to a meta box by use the
wp_update_post to a custom post type (call) with a custom taxonomy (call-type).
It then also updated the terms and taxonomy of the post.
The code work 100% for this.

Here is the working code

//update post
$post_id = wp_update_post($post_information);
if($post_id)
{
// Update Custom Meta
update_post_meta($post_id, ‘_call_16’, $_POST[‘call_16’]);
update_post_meta($post_id, ‘_call_17’, $_POST[‘call_17’]);
update_post_meta($post_id, ‘_call_18’, $_POST[‘call_18’]);
update_post_meta($post_id, ‘_call_19’, $_POST[‘call_19’]);
// Update Custom Meta
update_post_meta($post_id, ‘_call_20’, $_POST[‘call_20’]);
// Update call “terms” or taxonomy
$term_ids = array( 3, 14 );
$taxonomy = ‘call-type’;
wp_set_object_terms( $post_id, $term_ids, $taxonomy );
// Redirect
wp_redirect( ‘$url’ );

}
?>

the Html From


<div class=”form-group”>
<label class=”w-25″ for=”call_16″>Is site Visit Required</label>
<select class=”w-50 px-1 py-1″ name=”call_16″>Site Visit </p>
<option value=””></option>
<option value=”Yes”>Yes</option>
<option value=”No”>No</option>
</select>
</div>

The Problem

I would like the code to update the terms base on a value of the submitted entry.

The desired result is.

If $_POST[‘call_16’] = No change term to 18.
If $_POST[‘call_16’] = Yes change term to 14.

The code I have try

//update post
$post_id = wp_update_post($post_information);
if($post_id)
{
// Update Custom Meta
update_post_meta($post_id, ‘_call_16’, $_POST[‘call_16’]);
update_post_meta($post_id, ‘_call_17’, $_POST[‘call_17’]);
update_post_meta($post_id, ‘_call_18’, $_POST[‘call_18’]);
update_post_meta($post_id, ‘_call_19’, $_POST[‘call_19’]);
// Update call terms or taxonomy
if (isset($_POST[‘call_16’]) == ‘No’ ) {
$term_ids = array( 3, 18 );
$taxonomy = ‘call-type’;
wp_set_object_terms( $post_id, $term_ids, $taxonomy );
// Redirect
wp_redirect( ‘$url2’ );
}
else {
$term_ids = array( 3, 14 );
$taxonomy = ‘call-type’;
wp_set_object_terms( $post_id, $term_ids, $taxonomy );
// Redirect
wp_redirect( ‘$url’ );
}
}
?>

Can someone please advice if this is possible and how to do it in detail please.

.

Read more here:: Change the term based on the value of a $variable using wp_update_post in submitting a form

Leave a Reply

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