I had this working a couple of weeks ago and the only thing I can think is that WordPress has updated and broken the functionality?
I followed the tutorial here to add custom fields: http://net.tutsplus.com/tutorials/wordpress/rock-solid-wordpress-3-0-themes-using-custom-post-types/
It was working fine (I entered about 50 custom posts all with custom fields) but when I go to edit it today any changes I make to the custom fields are not being saved, although other changes work fine.
Does anyone know if this is a known bug?
Edit: Here is the code, added in functions.php to a brand new install (theme: twentyten)
add_action('init', 'testimonials_register');
add_action("admin_init", "admin_init");
add_action('save_post', 'save_testimonial');
function testimonials_register() {
$args = array(
'label' => __('Testimonials'),
'singular_label' => __('Testimonial'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'menu_position' => 5,
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail','custom-fields', 'revisions', 'excerpt', 'page-attributes')
);
register_post_type( 'testimonials' , $args );
}
register_taxonomy( 'testimonial_project_type', array("testimonials") , array( 'hierarchical' => true, 'label' => 'Project Type', 'query_var' => true, 'rewrite' => true ) );
function admin_init(){
add_meta_box("testimonialInfo-meta", "Testimonial Options", "meta_options_testimonial", "testimonials", "advanced", "high");
}
function meta_options_testimonial(){
global $post;
$custom = get_post_custom($post->ID);
$name = $custom["name"][0];
$position = $custom["position"][0];
$project_url = $custom["project_url"][0];
$website = $custom["website"][0];
?>
<label for="name" style="width:90px;display:inline-block">Name:</label> <input size="50" name="name" id="name" value="<?php echo $name; ?>" /><br>
<label for="position" style="width:90px;display:inline-block">Position:</label> <input size="50" name="position" id="position" value="<?php echo $position; ?>" /><br>
<label for="website" style="width:90px;display:inline-block">Website Name:</label> <input size="50" name="website" id="website" value="<?php echo $website; ?>" /><br>
<label for="project_url" style="width:90px;display:inline-block">Project slug:</label> <input size="50" name="project_url" id="project_url" value="<?php echo $project_url; ?>" /> <small>E.g. 'parker-harris'</small><br>
<?php
}
function save_testimonial(){
$custom_meta_fields = array( 'project_url','name','position','website');
foreach( $custom_meta_fields as $custom_meta_field ):
if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""):
update_post_meta($post->ID, $custom_meta_field, $_POST[$custom_meta_field]);
endif;
endforeach;
}
add_filter("manage_edit-testimonials_columns", "testimonials_edit_columns");
add_action("manage_posts_custom_column", "testimonials_custom_columns");
function testimonials_edit_columns($columns){
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Testimonial Title",
"name" => "Name",
"description" => "Excerpt",
"project_url" => "Project Slug"
);
return $columns;
}
function testimonials_custom_columns($column){
global $post;
switch ($column)
{
case "name":
$custom = get_post_custom();
echo $custom["name"][0].", ".$custom["position"][0]."<br> ".$custom["website"][0];
break;
case "description":
the_excerpt();
break;
case "project_url":
$custom = get_post_custom();
echo "<a target='_blank' href='/portfolio/".$custom["project_url"][0]."'>".$custom["project_url"][0]."</a>";
break;
}
}
// This shows testimonials in blog and feed.
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] || is_feed() )
$query->set( 'post_type', array( 'post', 'testimonials') );
return $query;
}