update wp_set_post_terms not working in wordpress

Actually I am using wp job manager plugin. The plugin has only one custom taxonomy
Here is the code to register taxonomy in plugin

register_taxonomy( "job_listing_type",
            array( "job_listing" ),
            array(
                'hierarchical'          => true,
                'label'                 => $plural,
                'labels' => array(
                    'name'              => $plural,
                    'singular_name'     => $singular,
                    'search_items'      => sprintf( __( 'Search %s', 'wp-job-manager' ), $plural ),
                    'all_items'         => sprintf( __( 'All %s', 'wp-job-manager' ), $plural ),
                    'parent_item'       => sprintf( __( 'Parent %s', 'wp-job-manager' ), $singular ),
                    'parent_item_colon' => sprintf( __( 'Parent %s:', 'wp-job-manager' ), $singular ),
                    'edit_item'         => sprintf( __( 'Edit %s', 'wp-job-manager' ), $singular ),
                    'update_item'       => sprintf( __( 'Update %s', 'wp-job-manager' ), $singular ),
                    'add_new_item'      => sprintf( __( 'Add New %s', 'wp-job-manager' ), $singular ),
                    'new_item_name'     => sprintf( __( 'New %s Name', 'wp-job-manager' ),  $singular )
                ),
                'show_ui'               => true,
                'query_var'             => true,
                'capabilities'          => array(
                    'manage_terms'      => $admin_capability,
                    'edit_terms'        => $admin_capability,
                    'delete_terms'      => $admin_capability,
                    'assign_terms'      => $admin_capability,
                ),
               'rewrite'                => $rewrite,
            )
        );

Then I added Three more taxonomies using

Read More
function skw_build_taxonomies() {



    //Sports
     $brand_args = array(
     'label' => 'Sports',
     'hierarchical' => true,
     'query_var' => true,
     'rewrite' => array('slug' => 'sport-cat')
     );
     register_taxonomy( 'sport-cat', 'job_listing', $brand_args );

      //Arts
     $brand_args1 = array(
     'label' => 'Arts & Music',
     'hierarchical' => true,
     'query_var' => true,
     'rewrite' => array('slug' => 'art-cat')
     );
     register_taxonomy( 'art-cat', 'job_listing', $brand_args1 );

     //Academic & Tech
     $brand_args2 = array(
     'label' => 'Academic & Tech',
     'hierarchical' => true,
     'query_var' => true,
     'rewrite' => array('slug' => 'academic-cat')
     );
     register_taxonomy( 'academic-cat', 'job_listing', $brand_args2 );

    }
    add_action( 'init', 'skw_build_taxonomies', 0 );

Instead of this code I also used the plugin’s code by changing taxonomy name and slug.
Now after inserting post, this code sets taxonomy successfully.
But when I want to update the post by using a custom form at front end, it adds the term related to taxonomy job_listing_type to all the four taxonomies and sets it. and sets the other three taxonomy terms correctly.
Here is my code to set the terms.

<input type="checkbox" name="_job_academic[]" value="<?php echo $term->id; ?>" <?php if(in_array($term->slug,$slug)) { echo 'checked="checked"'; } ?> />&nbsp;&nbsp;<?php echo $term->name ?></span>//sample html in the form

and here is php code to set the terms

wp_set_post_terms( $postid, $_POST['_job_type'], 'job_listing_type' );
            wp_set_post_terms( $postid, $_POST['_job_arts'], 'art-cat' );
            wp_set_post_terms( $postid, $_POST['_job_academic'], 'academic-cat' );
            wp_set_post_terms( $postid, $_POST['_job_sports'], 'sport-cat' );

I also tested this but same result

wp_set_object_terms( $postid, $_POST['_job_type'], 'job_listing_type', false );
            wp_set_object_terms( $postid, $_POST['_job_arts'], 'art-cat', false );
            wp_set_object_terms( $postid, $_POST['_job_academic'], 'academic-cat', false );
            wp_set_object_terms( $postid, $_POST['_job_sports'], 'sport-cat', false );

for example
I have four taxonomies named x1, x2, x3, x4

x1 has terms a, b, c, d
x2 has terms e, f, g, h
x3 has terms i, j, k, l
x4 has terms m, n, o, p

at front end, these terms are shown with checkboxes. So suppose I have checked a, e, i, m terms. So all of these should be set for that post. Bet wp_set_post_terms adds the term of X1 to all other taxonomies. Here is the final result

x1 has terms a, b, c, d, d
x2 has terms e, f, g, h, d
x3 has terms i, j, k, l, d
x4 has terms m, n, o, p, d

Related posts

Leave a Reply