Connecting a custom taxonomy with a custom meta box by code.
-
I houve found a couple of good code snippets. I created a new not hierarchical Taxonomy and a new meta box, but they cant “speak” each other, someone can send me the solution? I put here the code I have
// Event taxonomies add_action( 'init', function() { $labels = array( 'name' => _x( 'Ospiti', 'taxonomy general name' ), 'singular_name' => _x( 'Ospite', 'taxonomy singular name' ), ); register_taxonomy( 'numero_ospiti', array( 'post' ), array( 'hierarchical' => false, 'labels' => $labels, 'meta_box_cb' => "post_categories_meta_box", 'show_admin_column' => true, 'public' => true, ) ); } ); add_action( 'admin_head', function() { ?> <style type="text/css"> #newtaxonomy_name_parent { display: none; } </style> <?php }); add_action( 'admin_init', function() { if( isset( $_POST['tax_input'] ) && is_array( $_POST['tax_input'] ) ) { $new_tax_input = array(); foreach( $_POST['tax_input'] as $tax => $terms) { if( is_array( $terms ) ) { $taxonomy = get_taxonomy( $tax ); if( !$taxonomy->hierarchical ) { $terms = array_map( 'intval', array_filter( $terms ) ); } } $new_tax_input[$tax] = $terms; } $_POST['tax_input'] = $new_tax_input; } }); And now the meta box //creating a META BOX function custom_meta_box_markup($object) { ?> <div> <label for="meta-box-text">Text</label> <input name="meta-box-text" type="text" value="<?php echo get_post_meta($object->ID, "meta-box-text", true); ?>"> <br> <label for="meta-box-dropdown">Numero Ospiti</label> <select name="meta-box-dropdown"> <?php $option_values = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,); foreach($option_values as $key => $value) { if($value == get_post_meta($object->ID, "meta-box-dropdown", true)) { ?> <option selected><?php echo $value; ?></option> <?php } else { ?> <option><?php echo $value; ?></option> <?php } } ?> </select> <br> <label for="meta-box-checkbox">Numero ospiti</label> <?php $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true); if($checkbox_value == "") { ?> <input name="meta-box-checkbox" type="checkbox" value="true"> <?php } else if($checkbox_value == "true") { ?> <input name="meta-box-checkbox" type="checkbox" value="true" checked> <?php } ?> </div> <?php } function add_custom_meta_box() { add_meta_box("demo-meta-box", "Ospiti", "custom_meta_box_markup", "post", "side", "high", null); } add_action("add_meta_boxes", "add_custom_meta_box");
In particular I’m interested to the drop-down method. Here I have also the code to save and store the meta data
function save_custom_meta_box($post_id, $post, $update) { if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta-box-nonce"], basename(__FILE__))) return $post_id; if(!current_user_can("edit_post", $post_id)) return $post_id; if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) return $post_id; $slug = "post"; if($slug != $post->post_type) return $post_id; $meta_box_text_value = ""; $meta_box_dropdown_value = ""; $meta_box_checkbox_value = ""; if(isset($_POST["meta-box-text"])) { $meta_box_text_value = $_POST["meta-box-text"]; } update_post_meta($post_id, "meta-box-text", $meta_box_text_value); if(isset($_POST["meta-box-dropdown"])) { $meta_box_dropdown_value = $_POST["meta-box-dropdown"]; } update_post_meta($post_id, "meta-box-dropdown", $meta_box_dropdown_value); if(isset($_POST["meta-box-checkbox"])) { $meta_box_checkbox_value = $_POST["meta-box-checkbox"]; } update_post_meta($post_id, "meta-box-checkbox", $meta_box_checkbox_value); } add_action("save_post", "save_custom_meta_box", 10, 3);
I hope someone can make connections with all the codes I wrote. I’d like the have in the drop down the values of my new taxonomy ‘numero_ospiti’
The page I need help with: [log in to see the link]
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Connecting a custom taxonomy with a custom meta box by code.’ is closed to new replies.