0

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.

.

1 Answer 1

0

Your problem is this line:

if (isset($_POST['call_16']) == 'No' ) {

isset() returns true or false based on whether the 'call_16' item exists in the $_POST array. It doesn't return the value. So isset($_POST['call_16']) is true, not 'No'. If you want to check if it's set and that it has a specific value (which you should), then you need to check them separately:

if (isset( $_POST['call_16'] ) && 'No' === $_POST['call_16'] ) {

Although, it should be said that you've already used $_POST['call_16'] earlier, so the check for whether or not it's needs to happen earlier, and then just check the value for this specific condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.