最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Change the term based on the value of a $variable using wp_update_post in submitting a form

programmeradmin0浏览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.

.

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.

.

Share Improve this question asked Sep 8, 2019 at 6:13 MFWebMasterMFWebMaster 114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论