Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Why wont the custom fields from this post type save?

I have a custom post-type called Photos and a couple of meta boxes. Here is basically what I have in my functions.php

add_action('init', 'photo_register');

function photo_register() {

    $labels = array(
        'name' => _x('My Photos', 'post type general name'),
        'singular_name' => _x('Photo', 'post type singular name'),
        'add_new' => _x('Add New', 'photo item'),
        'add_new_item' => __('Add New Photo'),
        'edit_item' => __('Edit Photo'),
        'new_item' => __('New Photo'),
        'view_item' => __('View Photo'),
        'search_items' => __('Search Photos'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        //'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 4,
        'taxonomies' => array('post_tag','category'),
        'supports' => array('title','comments','trackbacks','revisions','custom-fields','page-attributes','thumbnail', 'excerpt', 'tags')
      ); 

    register_post_type( 'photo' , $args );
    register_taxonomy("Photos", array("photo"), array("hierarchical" => true, "label" => "Photos", "singular_label" => "Photo", "rewrite" => true));


}

add_action("admin_init", "admin_init");
add_action('save_post', 'save_details');

function admin_init(){ // add_meta_box( $id, $title, $callback, $page, $context, $priority ); 
  add_meta_box("media", "Media Type", "media", "photo", "side", "high");
  add_meta_box("map_meta", "Mapping Info", "map_meta", "photo", "normal", "high");
  add_meta_box("photo_meta", "Add a photograph", "photo_meta", "photo", "normal", "high");
}

function media(){
  global $post;
  $custom = get_post_custom($post->ID);
  $film = $custom["film"][0];
  $camera = $custom["camera"][0];

  ?>
  <label>Camera:</label>
  <input name="camera" value="<?php echo $camera; ?>" />

    <label>Film:</label>
  <input name="film" value="<?php echo $film; ?>" />

  <?php
}

function photo_meta(){
  global $post;
  $custom = get_post_custom($post->ID);
  $single_photo = $custom["single_photo"][0];
  ?>

  <div id="singleUpload">

    <div class="sUinput">
        <input id="single_photo" name="single_photo" value="<?php echo $single_photo; ?>" />
    </div>
    <div class="sUbutton">
    <input type="button" value="Upload" name="upload" id="upload_image_button" />
    </div>
  </div>

  <?php
}


function map_meta() {
  global $post;
  $custom = get_post_custom($post->ID);
  $latitude = $custom["latitude"][0];
  $longitude = $custom["longitude"][0];

  ?>
<div id="mapControls">

<div class="lat">
  <p><label for="lat">Latitude:</label><br />
  <input id="latitude"  name="latitude" value="<?php echo $latitude; ?>"></input></p>
 </div>
 <div class="lng">
  <p><label>Longitude:</label><br />
  <input id="longitude" name="longitude" value="<?php echo $longitude; ?>" ></input></p>
 </div>

   <?php }

  function save_details(){
  global $post;
  update_post_meta($post->ID, "single_photo", $_POST["single_photo"]);
  update_post_meta($post->ID, "latitude", $_POST["latitude"]);
  update_post_meta($post->ID, "longitude", $_POST["longitude"]);
  update_post_meta($post->ID, "camera", $_POST["camera"]);
  update_post_meta($post->ID, "film", $_POST["film"]);

}

I had it working for a while but now it will not save the fields and a refresh wipes everything. Can anyone see what the problem is? Any suggestions on how to find the bug? Basically anything you can think to improve this would be much appreciated, especially if I can get it to work!

EDIT

I figured out that they do save but if I remain on a post long enough for an autosave to run, then try to navigate away I get the message :

The page is asking you to confirm that you want to leave - data you have entered may not be saved.

I click leave then when I return to the post everything is erased. What is going on here?

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

I use the following code to prevent updates of my custom fields during auto-saves, ajax requests (quick edit) and bulk edits.

add_action('save_post', 'save_my_post');
function save_my_post($post_id)
{
    // Stop WP from clearing custom fields on autosave,
    // and also during ajax requests (e.g. quick edit) and bulk edits.
    if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') && DOING_AJAX) || isset($_REQUEST['bulk_edit']))
        return;

    // Clean, validate and save custom fields
}

Also see: How to prevent custom fields from being cleared during a bulk edit?

share|improve this answer
    
You and velvet both gave the correct answers for me... thanks! I am going to accept yours seeing that you gave me a link to you asking a very similar question. –  zac Jan 7 '12 at 5:12
add comment

You need to ignore all autosave requests. Modify your save function like this:

function save_details(){
   if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

   global $post;
   update_post_meta($post->ID, "single_photo", $_POST["single_photo"]);
   update_post_meta($post->ID, "latitude", $_POST["latitude"]);
   update_post_meta($post->ID, "longitude", $_POST["longitude"]);
   update_post_meta($post->ID, "camera", $_POST["camera"]);
   update_post_meta($post->ID, "film", $_POST["film"]);
}

Also, you should definitely add a bit more validation so that the function cannot be called in situations other than what it is intended for. I usually validate post type, user permissions, and nonces.

share|improve this answer
add comment

You need to isolate your save_post action. You also aren't passing in the post_id as an argument to the function.

function save_details($post_id){
    $post =& $_REQUEST;
    if($post['post_type'] == 'post'){
        update_post_meta($post_id, "single_photo", $_POST["single_photo"]);
    }
}
share|improve this answer
    
Thanks, I tried that but still having the same problem. What do you mean by "isolate your save_post action"? –  zac Jan 5 '12 at 6:41
    
I mean save_post is used by all post types. So if you don't want posts, pages, etc... to have this specific post meta, you need to tell the function to only update when the post_type is equal to your portfolio. You can also add an underscore prefix to keep these values hidden in the native custom field manager. –  Brian Fegter Jan 5 '12 at 6:51
    
No that is not a problem for me. The meta only shows up in my portfolio/photo type. I updated my question, I think the problem is with the auto-save. –  zac Jan 6 '12 at 7:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.