Custom meta box
-
Hi,
I want to add a ‘custom meta box’ to a ‘custom post type’.
The ‘custom meta box’ is a select list with titles from another ‘custom post type’. By example: I have the custom post types regions and countries, in the custom post type regions I want to select the country.Everything seems to work fine, except the slug changes everytime when I update the post. It changes to the latest option in the select list.
By example: the selectlist is: ‘Belgium – Germany – Austria’ than the slug changes to Austria.Any ideas how to fix this?
Here is my code:add_action("admin_init", "admin_init"); add_action('save_post', 'save_country'); function admin_init(){ add_meta_box("region-meta", "Land", "meta_options", "regio", "side", "low"); } function meta_options(){ global $post; $custom = get_post_custom($post->ID); $country = $custom["country"][0]; $args = array ( 'post_type' => 'land' ); echo '<select name="country">'; echo '<option value=" ">-- Selecteer een land -- </option>'; $query = new WP_Query( $args ); while($query->have_posts() ) : $query->the_post(); if(the_slug() == $country) : echo '<option value="'.the_slug().'" selected>' . get_the_title() . '</option>'; else : echo '<option value="'.the_slug().'">' . get_the_title() . '</option>'; endif; endwhile; echo '</select>'; } function save_country(){ global $post; update_post_meta($post->ID, "country", $_POST["country"]); }
Thanks.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Custom meta box’ is closed to new replies.