create post type with meta fields

Create a post type and add meta fields.

 array(
					'name' => __( 'Locations' ),
					'singular_name' => __( 'Location' ),
					'add_new' => __( 'Add New Location' ),
					'add_new_item' => __( 'Add New Location' ),
	               	'edit_item' => __( 'Edit Location' ),
	               	'new_item' => __( 'Add New Location' ),
	               	'view_item' => __( 'View Location' ),
	               	'search_items' => __( 'Search Location' ),
	               	'not_found' => __( 'No locations found' ),
	               	'not_found_in_trash' => __( 'No locations found in trash')
				),
			'public' => true,
			'rewrite' => array('slug' => 'location'),
			'taxonomies' => array('category'),
			'publicly_queryable' => true,
			'supports' => array('title', 'thumbnail', 'editor', 'revisions'),
			'register_meta_box_cb' => 'add_locations_metaboxes'
			)
		);
	}
	
	 Add the locations Meta Boxes
	function add_locations_metaboxes() {
	    add_meta_box('wpt_locations_information', 'Location Information', 'wpt_locations_information', 'locations', 'normal', 'high');
	}
	
	 create the html
	function wpt_locations_information() {
		global $post;
		echo '';
		
		 get the data
		$city_state = get_post_meta($post->ID, '_city_state', true);
		$latitude = get_post_meta($post->ID, '_latitude', true);
		$longitude = get_post_meta($post->ID, '_longitude', true);
		
		 Echo out the field
		echo '

Enter the City, State (ex. Los Angeles, CA):

'; echo ''; echo '

What is the Latitude? (this populates the map, How do I find this?)

'; echo ''; echo '

What is the Longitude?

'; echo ''; } Save the Metabox Data function wpt_save_locations_meta($post_id, $post) { 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['eventmeta_noncename'], plugin_basename(__FILE__) )) { return $post->ID; } Is the user allowed to edit the post or page? if ( !current_user_can( 'edit_post', $post->ID )) return $post->ID; OK, we're authenticated: we need to find and save the data We'll put it into an array to make it easier to loop though. $events_meta['_city_state'] = $_POST['_city_state']; $events_meta['_latitude'] = $_POST['_latitude']; $events_meta['_longitude'] = $_POST['_longitude']; Add values of $events_meta as custom fields foreach ($events_meta as $key => $value) { Cycle through the $events_meta array! if( $post->post_type == 'revision' ) return; Don't store custom data twice $value = implode(',', (array)$value); If $value is an array, make it a CSV (unlikely) if(get_post_meta($post->ID, $key, FALSE)) { If the custom field already has a value update_post_meta($post->ID, $key, $value); } else { If the custom field doesn't have a value add_post_meta($post->ID, $key, $value); } if(!$value) delete_post_meta($post->ID, $key); Delete if blank } } add_action('save_post', 'wpt_save_locations_meta', 1, 2); save the custom fields * get locations list * function get_location_list() { $args = array( 'post_type' => 'locations', 'post_status' => 'publish', ); $posts = get_posts($args); $i=1; foreach ($posts as $post) { if ($i=1){ $html .= '

  • '.$post->post_title.'

    '.get_post_meta($post->ID, '_city_state', true).'

  • '; } else if ($i=3){ $html .= '
  • '.$post->post_title.'

    '.get_post_meta($post->ID, '_city_state', true).'

  • '; $i=0; } else { $html .= '
  • '.$post->post_title.'

    '.get_post_meta($post->ID, '_city_state', true).'

  • '; } $i++; } return $html; } * get locations script * function get_location_script() { $args = array( 'post_type' => 'locations', 'post_status' => 'publish', ); $posts = get_posts($args); foreach ($posts as $post) { $latitude = get_post_meta($post->ID, '_latitude', true); $longitude = get_post_meta($post->ID, '_longitude', true); $js .= 'marker_'.$post->ID.' = new google.maps.Marker({ map:map, draggable:true, animation: google.maps.Animation.DROP, position: new google.maps.LatLng('.$latitude.', '.$longitude.'), }); google.maps.event.addListener(marker_'.$post->ID.', "click", function() { if(infowindow){infowindow.close();} infowindow = new google.maps.InfoWindow({ content: "

    '.$post->post_content.'

    ", }); map.setZoom(15); map.panTo(new google.maps.LatLng('.$latitude.', '.$longitude.')); infowindow.open(map,marker_'.$post->ID.'); });'; } return $js; } ?>