Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Update Custom Taxonomy Term from front end form WordPress

  • SOLVED

I can't update my custom post custom taxonomy term using my front end form. The headers shows me 200 Ok status but the update post function is not working.

I've been using:

wp_set_object_terms

and

wp_set_post_terms

in an attempt to make it work.

I did a little research here and I found some information but haven't been able to update my data.

I'm retrieving the terms on the front end form using this code:


<select name="tax_input[booking_status][]" class="res_status">
<?php
$tax_terms = get_terms('booking_status', array('hide_empty' => '0'));
foreach ( $tax_terms as $tax_term ):
echo '<option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';
endforeach;
?>
</select>


and also tried using this code:


<?php
$taxonomy = 'booking_status';

$selected_id = get_post_meta(
$post->ID,
'tax_input[booking_status][]',
true
);

wp_dropdown_categories( array(
'show_option_all' => __('Choose an status','wpshaper'),
'show_option_none' => '',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 0,
'child_of' => 0,
'exclude' => '',
'echo' => 1,
'selected' => $selected_id,
'hierarchical' => 1,
'name' => 'tax_input[booking_status][]',
'id' => $id,
'class' => 'res_status',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => $taxonomy,
'hide_if_empty' => true
) );
?>


Same as before I've tried with both codes in an attempt to make it work (both of them returned me the correct HTML, the first one had the value="taxonomy ID" and the second one as value="taxonomy name".

I've tried setting the custom taxonomy as hierarchical and non hierarchical.

The ajax code I'm using to update the post is this:


jQuery('document').ready(function($) {

$('.reservationEdit').on('submit', function(e) {
e.preventDefault();
var first_name = $(this).find('.first_name').val();
var last_name = $(this).find('.last_name').val();
var phone = $(this).find('.phone').val();
var mobile = $(this).find('.mobile').val();
var email = $(this).find('.email').val();
var post_id = $(this).find('.post_id').val();
var res_date = $(this).find('.res_date').val();
var res_date_time = $(this).find('.res_date_time').val();
var res_status = $(this).find('.res_status option:selected').val();
var res_status_html = $(this).find('.res_status option:selected').html();

// ajax codes for submission

$.ajax({
type: 'POST',
context: this,
url: ajaxurl,
data: {
"action": "SubmitReservation",
"first_name": first_name,
"last_name": last_name,
"mobile": mobile,
"email": email,
"post_id": post_id,
"res_date": res_date,
"res_date_time":res_date_time,
"res_status": res_status
},

success: function(data) {

$(".tr_" + post_id.toString()).find('td:nth-child(2)').html(first_name + ' ' + last_name);
$(".tr_" + post_id.toString()).find('td:nth-child(3)').html(mobile);
$(".tr_" + post_id.toString()).find('td:nth-child(4)').html(res_date + ' a las ' + res_date_time);
$(".tr_" + post_id.toString()).find('td:nth-child(5)').html(res_status_html);
//$(".tr_" + post_id.toString()).find('td:nth-child(6) a').html(email);
$.magnificPopup.close();
}
});

});

});



The update function is this one:


function post_ajax_submit() {

// get values

$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$mobile = trim($_POST['mobile']);
$email = trim($_POST['email']);
$postid = trim($_POST['post_id']);
$res_date = trim($_POST['res_date']);
$res_date_time = trim($_POST['res_date_time']);
$res_status = trim($_POST['tax_input[booking_status][]']);
$postid = (int) $postid;

update_post_meta($postid, '_sendersName', $first_name);
update_post_meta($postid, '_sendersLastName', $last_name);
update_post_meta($postid, '_sendersPhone', $mobile);
update_post_meta($postid, '_sendersEmail', $email);
update_post_meta($postid, '_resDate', $res_date);
update_post_meta($postid, '_resDateTime', $res_date_time);

wp_set_object_terms($post_id, $res_status, 'booking_status', false);

// also using this one

wp_set_post_terms($post_id, $res_status, 'booking_status', false);

}

add_action('wp_ajax_SubmitReservation', 'booking_ajax_submit');
add_action('wp_ajax_nopriv_SubmitReservation', 'booking_ajax_submit');


The update function is working for all my data except the taxonomy term. Any help will be deeply appreciated. Thanks.

Answers (3)

2017-01-11

Bob answers:

I think, while you are sending value as res_status, you should use the same for getting it back in post.
"res_status": res_status


$res_status = trim($_POST['res_status']);


.


Bob comments:

you should also check if that it is getting proper value using jquery and sending it.
do write alert() code and check it also.
I think below code should also work

var res_status = $(this).find('.res_status').val();


I think wp_set_post_terms is used for native post type only. so you have to use only wp_set_object_terms.


Alvaro Rosado comments:

I did put it like this:

var res_status = $(this).find('.res_status').val();

and alert('res_status');

returns me the ID value of the term, the problem persist in the update function :(


Bob comments:

Can you provide taxonomy and form code so that I can create environment in my localhost and send you fixed/modified code?

you can email me at [email protected]


Bob comments:

For non-hierarchical terms(tag like term) you must use array with id.

example

$tag = array( 5 );
wp_set_post_terms( $post_id, $tag, $taxonomy );


Alvaro Rosado comments:

Hi bro, It's hierarchical (like Category).

This is my taxonomy code:

// Register Custom Taxonomy
function booking_taxonomy() {

$labels = array(
'name' => _x( 'Estatus', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Estatus', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Estatus', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'New Item Name', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Items', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'booking_status', array( 'booking' ), $args );

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


This is the form code with some changes you have suggested:

<form class="reservationEdit" action="" method="POST">

<div class="modal-body">

<div class="form-group form-field">
<label for="first_name">Nombre</label>
<input type="text" name="first_name" class="form-control first_name" value="<?php echo get_post_meta(get_the_ID(), '_sendersName', true); ?>" id="name" >
</div>

<div class="form-group form-field">
<label for="last_name">Apellido</label>
<input type="text" name="last_name" class="form-control last_name" value="<?php echo get_post_meta(get_the_ID(), '_sendersLastName', true); ?>" id="lastName" >
</div>

<div class="form-group form-field">
<label for="mobile">Celular</label>
<input type="text" name="mobile" class="form-control mobile" value="<?php echo get_post_meta(get_the_ID(), '_sendersPhone', true); ?>" id="mobile" >
</div>

<div class="form-group form-field">
<label for="email">Email</label>
<input type="text" name="email" class="form-control email" value="<?php echo get_post_meta(get_the_ID(), '_sendersEmail', true); ?>" id="email" >
</div>

<div class="form-group form-field">
<label for="res_date">Fecha de la cita</label>
<input type="text" name="res_date" class="form-control res_date" value="<?php echo get_post_meta(get_the_ID(), '_resDate', true); ?>" id="resDate" >
</div>

<div class="form-group form-field">
<label for="res_time">Hora de la cita</label>
<select name="res_date_time" class="form-control res_date_time" id="resDateTime">
<option value="<?php echo get_post_meta(get_the_ID(), '_resDateTime', true); ?>" selected><?php echo get_post_meta(get_the_ID(), '_resDateTime', true); ?></option>
<option value="9:00 am">9:00 am</option>
<option value="9:30 am">9:30 am</option>
<option value="10:00 am">10:00 am</option>
<option value="10:30 am">10:30 am</option>
<option value="11:00 am">11:00 am</option>
<option value="11:30 am">11:00 am</option>
<option value="12:00 pm">12:00 am</option>
<option value="12:30 pm">12:30 am</option>
<option value="1:00 pm">1:00 pm</option>
<option value="1:30 pm">1:30 pm</option>
<option value="2:00 pm">2:00 pm</option>
<option value="2:30 pm">2:30 pm</option>
<option value="3:00 pm">3:00 pm</option>
<option value="3:30 pm">3:30 pm</option>
<option value="4:00 pm">4:00 pm</option>
<option value="4:30 pm">4:30 pm</option>
<option value="5:00 pm">5:00 pm</option>
<option value="5:30 pm">5:30 pm</option>
<option value="6:00 pm">6:00 pm</option>
<option value="6:30 pm">6:30 pm</option>
<option value="7:00 pm">7:00 pm</option>
<option value="7:30 pm">7:30 pm</option>
<option value="8:00 pm">8:00 pm</option>
<option value="8:30 pm">8:30 pm</option>
</select>
</div>

<div class="form-group form-field">
<label for="res_status">Estatus de la reservación</label>
<?php /* ?><select name="_resStatus" class="res_status">
<?php
$tax_terms = get_terms('booking_status', array('hide_empty' => '0'));
foreach ( $tax_terms as $tax_term ):
echo '<option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';
endforeach;
?>
</select> */ ?>
<?php

$taxonomy = 'booking_status';
$selected_id = get_post_meta( $post->ID, 'booking_status', true );

wp_dropdown_categories( array(
'show_option_all' => __('Choose an status','wpshaper'),
'show_option_none' => '',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 0,
'child_of' => 0,
'exclude' => '',
'echo' => 1,
'selected' => $selected_id,
'hierarchical' => 1,
'name' => 'booking_status',
'id' => $id,
'class' => 'res_status',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => $taxonomy,
'hide_if_empty' => true
) );

?>
</div>

<input type="hidden" name="post_id" class="post_id" value="<?php echo get_the_ID(); ?>" id="postID" >

</div>

<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><?php _e('Cancel','wpshaper'); ?></button>
<button type="submit" class="btn btn-primary"><?php _e('Save changes', 'wpshaper'); ?></button>
</div>

</form>


Bob comments:

You are using $postid to update post like
update_post_meta($postid, '_sendersName', $first_name);
but using $post_id for setting taxonomy.

wp_set_object_terms($post_id, $res_status, 'booking_status', false);


Bob comments:

here you are using booking_ajax_submit but your function name is post_ajax_submit.

add_action('wp_ajax_SubmitReservation', 'booking_ajax_submit');
add_action('wp_ajax_nopriv_SubmitReservation', 'booking_ajax_submit');


Bob comments:


function booking_ajax_submit() {

// get values

$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$mobile = trim($_POST['mobile']);
$email = trim($_POST['email']);
$postid = trim($_POST['post_id']);
$res_date = trim($_POST['res_date']);
$res_date_time = trim($_POST['res_date_time']);
$res_status = trim($_POST['res_status']);
$postid = (int) $postid;
$res_status1 = array();
$res_status1[] = (int)$res_status;
update_post_meta($postid, '_sendersName', $first_name);
update_post_meta($postid, '_sendersLastName', $last_name);
update_post_meta($postid, '_sendersPhone', $mobile);
update_post_meta($postid, '_sendersEmail', $email);
update_post_meta($postid, '_resDate', $res_date);
update_post_meta($postid, '_resDateTime', $res_date_time);

wp_set_object_terms($postid, $res_status1, 'booking_status', false);

// also using this one

//wp_set_post_terms($post_id, $res_status, 'booking_status', false);

}

add_action('wp_ajax_SubmitReservation', 'booking_ajax_submit');
add_action('wp_ajax_nopriv_SubmitReservation', 'booking_ajax_submit');


if ajaxurl is not defined and has error about it

add_action('wp_head', 'myplugin_ajaxurl');

function myplugin_ajaxurl() {

echo '<script type="text/javascript">
var ajaxurl = "' . admin_url('admin-ajax.php') . '";
</script>';
}


jquery code

jQuery('document').ready(function($) {

$('.reservationEdit').on('submit', function(e) {
e.preventDefault();
var first_name = $(this).find('.first_name').val();
var last_name = $(this).find('.last_name').val();
var phone = $(this).find('.phone').val();
var mobile = $(this).find('.mobile').val();
var email = $(this).find('.email').val();
var post_id = $(this).find('.post_id').val();
var res_date = $(this).find('.res_date').val();
var res_date_time = $(this).find('.res_date_time').val();
var res_status = $(this).find('.res_status').val();
var res_status_html = $(this).find('.res_status option:selected').html();

// ajax codes for submission

$.ajax({
type: 'POST',
context: this,
url: ajaxurl,
data: {
"action": "SubmitReservation",
"first_name": first_name,
"last_name": last_name,
"mobile": mobile,
"email": email,
"post_id": post_id,
"res_date": res_date,
"res_date_time":res_date_time,
"res_status": res_status
},

success: function(data) {

$(".tr_" + post_id.toString()).find('td:nth-child(2)').html(first_name + ' ' + last_name);
$(".tr_" + post_id.toString()).find('td:nth-child(3)').html(mobile);
$(".tr_" + post_id.toString()).find('td:nth-child(4)').html(res_date + ' a las ' + res_date_time);
$(".tr_" + post_id.toString()).find('td:nth-child(5)').html(res_status_html);
//$(".tr_" + post_id.toString()).find('td:nth-child(6) a').html(email);
$.magnificPopup.close();
}
});

});

});


Alvaro Rosado comments:

Let me try it in a few hours and I let you know... I can't believe I didn't see the Post ID $vars where different... :S


Alvaro Rosado comments:

Worked perfectly bro!!!. Would you be interested in provide me more help vía email?


Alvaro Rosado comments:

My email is [email protected]

2017-01-10

Rempty answers:

try changing
$res_status = trim($_POST['tax_input[booking_status][]']);
to
$res_status = $_POST['tax_input']['booking_status'];


Alvaro Rosado comments:

Nothing.

I used both functions.

wp_set_post_terms and wp_set_object_terms

Don't know if the ID or Name value has to do something. It's a Hierarchical Taxonomy so I'm using ID as a value, but I don't know If that's the proper way.


Rempty comments:

the problem is the name of the field change to other name like
booking_status[]
and use
$res_status = $_POST['booking_status'];


Alvaro Rosado comments:

Nope, didn't work :(

I read in a post that thats it's the way I should name that select or input.

$taxonomy = "booking_status";

then put in select name as this

"tax_input['.$taxonomy.'][]"

Also if you go to the admin panel and inspect the name of that taxonomy input it's this one:


Rempty comments:

Please take a screenshot of console network of your ajax petition, in special the post parameters sent


Alvaro Rosado comments:

This is the screenshot bro.


Rempty comments:

please try this

$res_status = $_POST['res_status'];
$res_status = explode(',',$res_status);
$res_status = array_map( 'intval', $res_status );


$update_status=wp_set_object_terms( $post_id, $res_status, 'booking_status' );

Also add this debug code at the end of your function, this will print the error in the response (console/network)
//Debug Code
var_dump($update_status);

2017-01-11

Arnav Joy answers:

are you still looking for help or is it solved ?


Alvaro Rosado comments:

No, I have not solved it. Any help it's really appreciated.