• 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)
  • Have you looked at the output value of the_slug? Maybe something goes wrong there…

    Thread Starter TA003

    (@ta003)

    When I print the code the html for the form looks fine, so i can’t figure out where it goes wrong.

    This is the function I use to get the slug:

    function the_slug() {
        $post_data = get_post($post->ID, ARRAY_A);
        $slug = $post_data['post_name'];
        return $slug;
    }

    This is the output of the wp_query:

    <select name="region">
    <option value=" ">-- Select option -- </option>
    <option value="test1">Test 1</option>
    <option value="test2">Test 2</option>
    <option value="test3">Test 3</option>
    </select>

    What value is saved? (Check under “Custom fields” – if not visible check the checkbox under “Screen options”).

    Thread Starter TA003

    (@ta003)

    I fixed it. The problem was the position of the meta box.
    When the meta box is in the sidebar, it changes the url.
    If the meta box is below the editor everything works fine.
    Verry strange.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom meta box’ is closed to new replies.