function room_facility_meta_save( $post_id )
{
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
//if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) { <-- THIS DIDN'T WORK
if(! isset( $_POST['room_facility_nonce'] )){
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
//wp_set_object_terms( 742, array(211), 'room_facility');
$taxonomy = "room_facility";
$term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
foreach ($term_facilities as $tax => $term)
{
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "_room_facility_key_".$room_facility_id;
//update postmeta room_facility_key_<ID>
if( isset( $_POST[ "text_".$room_facility_id] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
}
//set wp_term_relationships for room_type and room_facility
if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
{
//wp_set_post_terms( $post_id, $term, $taxonomy); <-- WRONG function
$error_obj = wp_set_object_terms( $post_id, array(int)$term->term_id) , $taxonomy);
} /*else {
wp_set_post_terms( $post_id, $term, $taxonomy);
}*/
}
}
add_action( 'save_post', 'room_facility_meta_save' );
function room_facility_meta_save( $post_id )
{
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
//if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) { <-- THIS DIDN'T WORK
if(! isset( $_POST['room_facility_nonce'] )){
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
//wp_set_object_terms( 742, array(211), 'room_facility');
$taxonomy = "room_facility";
$term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
foreach ($term_facilities as $tax => $term)
{
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "_room_facility_key_".$room_facility_id;
//update postmeta room_facility_key_<ID>
if( isset( $_POST[ "text_".$room_facility_id] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
}
//set wp_term_relationships for room_type and room_facility
if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
{
//wp_set_post_terms( $post_id, $term, $taxonomy); <-- WRONG function
wp_set_object_terms( $post_id, array($term->term_id) , $taxonomy);
} /*else {
wp_set_post_terms( $post_id, $term, $taxonomy);
}*/
}
}
add_action( 'save_post', 'room_facility_meta_save' );
function room_facility_meta_save( $post_id )
{
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
//if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) { <-- THIS DIDN'T WORK
if(! isset( $_POST['room_facility_nonce'] )){
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
//wp_set_object_terms( 742, array(211), 'room_facility');
$taxonomy = "room_facility";
$term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
foreach ($term_facilities as $tax => $term)
{
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "_room_facility_key_".$room_facility_id;
//update postmeta room_facility_key_<ID>
if( isset( $_POST[ "text_".$room_facility_id] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
}
//set wp_term_relationships for room_type and room_facility
if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
{
//wp_set_post_terms( $post_id, $term, $taxonomy); <-- WRONG function
$error_obj = wp_set_object_terms( $post_id, (int)$term->term_id , $taxonomy);
} /*else {
wp_set_post_terms( $post_id, $term, $taxonomy);
}*/
}
}
add_action( 'save_post', 'room_facility_meta_save' );
function room_facility_meta_save( $post_id )
{
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
//if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) { <-- THIS DIDN'T WORK
if(! isset( $_POST['room_facility_nonce'] )){
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
//wp_set_object_terms( 742, array(211), 'room_facility');
$taxonomy = "room_facility";
$term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
foreach ($term_facilities as $tax => $term)
{
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "room_facility_key_""_room_facility_key_".$room_facility_id;
//update postmeta room_facility_key_<ID>
if( isset( $_POST[ "text_".$room_facility_id] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
}
//set wp_term_relationships for room_type and room_facility
if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
{
//wp_set_post_terms( $post_id, $term, $taxonomy); <-- WRONG function
wp_set_object_terms( $post_id, array($term->term_id) , $taxonomy);
} /*else {
wp_set_post_terms( $post_id, $term, $taxonomy);
}*/
}
}
add_action( 'save_post', 'room_facility_meta_save' );
function room_facility_meta_save( $post_id )
{
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) {
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
//wp_set_object_terms( 742, array(211), 'room_facility');
$taxonomy = "room_facility";
$term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
foreach ($term_facilities as $tax => $term)
{
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "room_facility_key_".$room_facility_id;
//update postmeta room_facility_key_<ID>
if( isset( $_POST[ "text_".$room_facility_id] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
}
//set wp_term_relationships for room_type and room_facility
if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
{
wp_set_post_terms( $post_id, $term, $taxonomy);
} /*else {
wp_set_post_terms( $post_id, $term, $taxonomy);
}*/
}
}
add_action( 'save_post', 'room_facility_meta_save' );
function room_facility_meta_save( $post_id )
{
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
//if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) { <-- THIS DIDN'T WORK
if(! isset( $_POST['room_facility_nonce'] )){
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
//wp_set_object_terms( 742, array(211), 'room_facility');
$taxonomy = "room_facility";
$term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
foreach ($term_facilities as $tax => $term)
{
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "_room_facility_key_".$room_facility_id;
//update postmeta room_facility_key_<ID>
if( isset( $_POST[ "text_".$room_facility_id] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
}
//set wp_term_relationships for room_type and room_facility
if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
{
//wp_set_post_terms( $post_id, $term, $taxonomy); <-- WRONG function
wp_set_object_terms( $post_id, array($term->term_id) , $taxonomy);
} /*else {
wp_set_post_terms( $post_id, $term, $taxonomy);
}*/
}
}
add_action( 'save_post', 'room_facility_meta_save' );
wp_set_object_terms doesn't working issue with saving custom metaboxes fields
I created some custom input fields to the backend ofhave a post_type named room_type and a taxonomy room_facility. Therefore I createdBetween them I want a checkbox list to link the post_typerelationship. Furthermore I would like to add a taxonomyvalue for each relationship room_type and room_facility. BesideSo the checkbox therefrontend looks like this:
The value is stored in postmeta with a input fieldmeta_key room_facility_key, where is the id from room_facility and a post_id from room_type. I think this is the easiest way to savelink the value as metadata. Well savingto room_type and room_facility, right?
You can see I get the metadata is novalues stored in database. The problem, but is to save the taxonomy relationship in the wp_term_relationships table doesn't workthem. I tried like this Here is my code.
To add the metabox:
function room_facility_custom_meta() {
add_meta_box( 'room_facility_meta', __( 'Meta Box ', 'room_facility-textdomain' ), 'room_facility_meta_callback', 'room_type' );
}
add_action( 'add_meta_boxes', 'room_facility_custom_meta' );
My callback function
function room_facility_meta_callback( $post ) {
//nonce_field
wp_nonce_field( basename( __FILE__ ), 'room_facility_nonce' );
$tax = "room_facility";
$term_facilities = get_terms( $tax, array('hide_empty' => false));
$term_checked_facilities= wp_get_post_terms($post->ID, $tax);
foreach($term_facilities as $term){
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "_room_facility_key_".$room_facility_id;
//value from the postmeta
$room_facility_value = get_post_meta($post->ID, $meta_key);
?>
<div class="row">
<label for="checkbox_<?php echo $room_facility_id ?>">
<input type="checkbox" name="checkbox_<?php echo $room_facility_id ?>" id="checkbox_<?php echo $room_facility_id ?>" <?php checked( myArrayContainsWord($term_checked_facilities, $term->term_id)); ?> />
<?php _e( $term->name, 'prfx-textdomain' )?>
</label>
<input type="text" name="text_<?php echo $room_facility_id ?>" id="text_<?php echo $room_facility_id ?>" value="<?php if ( !empty ( $room_facility_value[0] ) ){ echo $room_facility_value[0]; }?>" />
</div>
<?php
} //end foreach
}
My save function
function room_facility_meta_save( $post_id )
{
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) {
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
//wp_set_object_terms( 742, array(211), 'facility''room_facility');
$taxonomy = "room_facility";
$term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
foreach ($term_facilities as $tax => $term)
{
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "room_facility_key_".$room_facility_id;
//update postmeta room_facility_key_<ID>
if( isset( $_POST[ "text_".$room_facility_id] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
}
//set wp_term_relationships for room_type and room_facility
if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
{
wp_set_post_terms( $post_id, $term, $taxonomy);
} /*else {
wp_set_post_terms( $post_id, $term, $taxonomy);
}*/
}
}
add_action( 'save_post', 'room_facility_meta_save' );
Its in the same methode like the saving ofI'm not sure if the metadatasave function is called, because when I do a
echo '<script>alert("test")</script>';
Nothing happens...
I also add the methodknow I posted a lot of code but I really don't Have an idea why its not working. Feel free to save_post actionask if you have a question. Am I missing something?
wp_set_object_terms doesn't working with custom fields
I created some custom input fields to the backend of a post_type. Therefore I created a checkbox list to link the post_type to a taxonomy. Beside the checkbox there a input field to save the value as metadata. Well saving the metadata is no problem, but to save the taxonomy relationship in the wp_term_relationships table doesn't work. I tried like this
wp_set_object_terms( 742, array(211), 'facility');
Its in the same methode like the saving of the metadata. I also add the method to save_post action. Am I missing something?
issue with saving custom metaboxes fields
I have a post_type named room_type and a taxonomy room_facility. Between them I want a relationship. Furthermore I would like to add a value for each relationship room_type and room_facility. So the frontend looks like this:
The value is stored in postmeta with a meta_key room_facility_key, where is the id from room_facility and a post_id from room_type. I think this is the easiest way to link the value to room_type and room_facility, right?
You can see I get the values stored in database. The problem is to save them. Here is my code.
To add the metabox:
function room_facility_custom_meta() {
add_meta_box( 'room_facility_meta', __( 'Meta Box ', 'room_facility-textdomain' ), 'room_facility_meta_callback', 'room_type' );
}
add_action( 'add_meta_boxes', 'room_facility_custom_meta' );
My callback function
function room_facility_meta_callback( $post ) {
//nonce_field
wp_nonce_field( basename( __FILE__ ), 'room_facility_nonce' );
$tax = "room_facility";
$term_facilities = get_terms( $tax, array('hide_empty' => false));
$term_checked_facilities= wp_get_post_terms($post->ID, $tax);
foreach($term_facilities as $term){
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "_room_facility_key_".$room_facility_id;
//value from the postmeta
$room_facility_value = get_post_meta($post->ID, $meta_key);
?>
<div class="row">
<label for="checkbox_<?php echo $room_facility_id ?>">
<input type="checkbox" name="checkbox_<?php echo $room_facility_id ?>" id="checkbox_<?php echo $room_facility_id ?>" <?php checked( myArrayContainsWord($term_checked_facilities, $term->term_id)); ?> />
<?php _e( $term->name, 'prfx-textdomain' )?>
</label>
<input type="text" name="text_<?php echo $room_facility_id ?>" id="text_<?php echo $room_facility_id ?>" value="<?php if ( !empty ( $room_facility_value[0] ) ){ echo $room_facility_value[0]; }?>" />
</div>
<?php
} //end foreach
}
My save function
function room_facility_meta_save( $post_id )
{
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['room_facility_nonce'], plugin_basename(__FILE__) )) {
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
//wp_set_object_terms( 742, array(211), 'room_facility');
$taxonomy = "room_facility";
$term_facilities = get_terms( $taxonomy, array('hide_empty' => false));
foreach ($term_facilities as $tax => $term)
{
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "room_facility_key_".$room_facility_id;
//update postmeta room_facility_key_<ID>
if( isset( $_POST[ "text_".$room_facility_id] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
}
//set wp_term_relationships for room_type and room_facility
if( isset( $_POST[ 'checkbox_'.$room_facility_id ] ) )
{
wp_set_post_terms( $post_id, $term, $taxonomy);
} /*else {
wp_set_post_terms( $post_id, $term, $taxonomy);
}*/
}
}
add_action( 'save_post', 'room_facility_meta_save' );
I'm not sure if the save function is called, because when I do a
echo '<script>alert("test")</script>';
Nothing happens...
I know I posted a lot of code but I really don't Have an idea why its not working. Feel free to ask if you have a question.