Possible to turn off autosave for single custom post type

So I’m having an issue with my custom fields in my custom post type. For whatever reason the fields save and then clear somewhat randomly… I’m sure it’s not random but I’m not sure what’s triggering this to happen. Here’s the code for my custom post type:

    // Custom Post Type: Strategic Giving
add_action('init', 'giving_register');

function giving_register() {

  $labels = array(
    'name' => _x('Invest Items', 'post type general name'),
    'singular_name' => _x('Item', 'post type singular name'),
    'add_new' => _x('Add New', 'giving item'),
    'add_new_item' => __('Add New Item'),
    'edit_item' => __('Edit Item'),
    'new_item' => __('New Item'),
    'view_item' => __('View Item'),
    'search_items' => __('Search Items'),
    '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() . '/images/cpt-giving.png',
    'rewrite' => array( 'slug' => 'giving_items' ),
    'capability_type' => 'post',
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array('title','thumbnail','editor'),
    'paged' => false,
    ); 

  register_post_type( 'givings' , $args );
}

register_post_type( 'givings' , $args );

add_action("admin_init", "giving_admin_init");

function giving_admin_init(){
  add_meta_box("giving_info-meta", "Item Options", "giving_info", "givings", "side", "high");
}

function giving_info(){
  global $post;
  $custom = get_post_custom($post->ID);
  $amount = $custom["amount"][0];
  $monthly = $custom["monthly"][0];
  $user_entered_value = $custom["user_entered_value"][0];
  $item_name = $custom["item_name"][0];
  $special = $custom["special"][0];
  ?>
  <div style="text-align: right;">
    <p>
      <label for="amount"><strong>Amount:</strong></label>  
      <input style="width: 180px" type="text" name="amount" id="amount" value="<?php echo $amount; ?>" />
      <em>Example: 30.00</em>
    </p>
    <p>
      <label for="monthly"><strong>Monthly?</strong></label>  
      <?php if ($monthly == 'on') { ?>
        <input type="checkbox" name="monthly" id="monthly" checked="checked" />
      <?php } else { ?>
        <input type="checkbox" name="monthly" id="monthly" />
      <?php } ?>      
    </p>
    <p>
      <label for="special"><strong>Special Item?</strong></label>  
      <?php if ($special == 'on') { ?>
        <input type="checkbox" name="special" id="special" checked="checked" />
      <?php } else { ?>
        <input type="checkbox" name="special" id="special" />
      <?php } ?>      
    </p>
    <p>
      <label for="user_entered_value"><strong>Allow Giver To Enter Custom Value?</strong></label>  
      <?php if ($user_entered_value == 'on') { ?>
        <input type="checkbox" name="user_entered_value" id="user_entered_value" checked="checked" />
      <?php } else { ?>
        <input type="checkbox" name="user_entered_value" id="user_entered_value" />
      <?php } ?>      
    </p>
    <p>
      <label for="item_name"><strong>Item Name:</strong></label>              
      <input style="width: 180px" type="text" name="item_name" id="item_name" value="<?php echo $item_name; ?>" /><br />
      If item is a <em>per item</em> then enter the name of the singular item. <em>Example: Chair - which will be displayed as "30.00 per Chair"</em>
    </p>
    <p style="text-align: left;">
      Strategic Giving photo must be horizontal and uploaded/set as the <strong>Featured Image</strong> (see below).
      <em>Do not add photo to content area.</em>
    </p>
  </div>
  <?php }  

add_action('save_post', 'giving_save_details_amount');
add_action('save_post', 'giving_save_details_monthly');
add_action('save_post', 'giving_save_details_user_entered_value');
add_action('save_post', 'giving_save_details_item_name');
add_action('save_post', 'giving_save_details_special');

function giving_save_details_amount(){
  global $post;
  update_post_meta($post->ID, "amount", $_POST["amount"]);
}

function giving_save_details_monthly(){
  global $post;
  update_post_meta($post->ID, "monthly", $_POST["monthly"]);
}

function giving_save_details_user_entered_value(){
  global $post;
  update_post_meta($post->ID, "user_entered_value", $_POST["user_entered_value"]);
}

function giving_save_details_item_name(){
  global $post;
  update_post_meta($post->ID, "item_name", $_POST["item_name"]);
}

function giving_save_details_special(){
  global $post;
  update_post_meta($post->ID, "special", $_POST["special"]);
}

add_action("manage_pages_custom_column",  "givings_custom_columns");
add_filter("manage_edit-givings_columns", "givings_edit_columns");

function givings_edit_columns($columns){
  $columns = array(
    "cb" => "<input type=\"checkbox\" />",
    "title" => "Strategic Giving Item",
    "amount" => "Amount",
    "monthly" => "Monthly",
    "special" => "Special Item",
    "giving_image" => "Image"
    );

  return $columns;
}

function givings_custom_columns($column){
  global $post;

  switch ($column) {
    case "amount":
    $custom = get_post_custom();
    echo $custom["amount"][0];
    break;

    case "monthly":
    $custom = get_post_custom();
    $is_monthly = $custom["monthly"][0];
    if ($is_monthly == "on") {
      echo "Yes";
    };
    break;

    case "special":
    $custom = get_post_custom();
    $is_special = $custom["special"][0];
    if ($is_special == "on") {
      echo "Yes";
    };
    break;

    case "giving_image":
      echo get_the_post_thumbnail(NULL, 'staff_admin');
    break;
  }
}

function giving_amount(){
  $custom = get_post_custom();
  return $custom["amount"][0];
}

function giving_monthly(){
  $custom = get_post_custom();
  return $custom["monthly"][0];
}

function giving_special(){
  $custom = get_post_custom();
  return $custom["special"][0];
}

function giving_user_entered_value(){
  $custom = get_post_custom();
  return $custom["user_entered_value"][0];
}

function giving_item_name(){
  $custom = get_post_custom();
  return $custom["item_name"][0];
}

Update: So I did more research and figured it out. Autosave (aka Revisions) – Post metadata deletes itself

Is it possible to turn off autosave for just a single post type and not globally?

3 s
3

That’s an easy one 🙂

add_action( 'admin_enqueue_scripts', 'my_admin_enqueue_scripts' );
function my_admin_enqueue_scripts() {
    if ( 'your_post_type' == get_post_type() )
        wp_dequeue_script( 'autosave' );
}

Leave a Comment