0

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:

enter image description here

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? enter image description here

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__) )) { <-- 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' );

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.

6
  • Im a little confused, if its saving in the database, that means the save_post function is working. Where are you having trouble?
    – stims
    Commented Oct 19, 2016 at 15:32
  • Sry for the confusion...I added the current data manually to test the callback function....
    – MolteNolte
    Commented Oct 20, 2016 at 10:26
  • but shouldn't the echo '<script>alert("test")</script>'; be called?
    – MolteNolte
    Commented Oct 20, 2016 at 11:05
  • Save post is fired before you can add anything to the DOM. You need to use something like error_log to debug it.
    – stims
    Commented Oct 20, 2016 at 12:09
  • Okay it turned out the if statement to verify the nonce didn't work. Now I'am using if(! isset( $_POST['room_facility_nonce'] )) and the saving of the postmeta is working. However since I tried to use the wp_set_post_terms function to save data in wp_term_relationships it couln't work. wp_set_object_terms should be the right one or? Unfortunately its still not working. Shouln't be right with these params: wp_set_object_terms(742,145,room_facility), where 742 is the post_id of the room_type, 145 is the room_facility?
    – MolteNolte
    Commented Oct 21, 2016 at 11:18

1 Answer 1

0

You have to register the taxonomy before you can add terms to a post type. Register the taxonomy after you register the custom post type. If you are not the one that is registering the post type, you will need to find where it is being registered and make sure you register the taxonomy afterwards. After both those are registered you can add terms to posts using wp_set_object_terms.

5
  • I did, but its still not working...
    – MolteNolte
    Commented Oct 12, 2016 at 10:29
  • Please post all necessary code.
    – stims
    Commented Oct 12, 2016 at 14:30
  • Hey thanks for you comment. I added a full explanation.
    – MolteNolte
    Commented Oct 19, 2016 at 14:55
  • Well in this thread stackoverflow.com/questions/32960214/…
    – MolteNolte
    Commented Oct 24, 2016 at 14:05
  • ...somebody is saying the taxnomie have to be registered before the post_type...however, both solutions didn't help me....but its confusing....
    – MolteNolte
    Commented Oct 24, 2016 at 14:07

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.