Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Wordpress

WordPress: Fix a Custom Field Resetting After Every Autosave

- 18.11.26 - ErcanOPAK

📝 WordPress: Fix a Custom Field Resetting After Every Autosave

WordPress’s autosave heartbeat fires every 60 seconds and saves a post revision in the background using its own AJAX request, which only posts the fields the editor actually serialized for that revision — not necessarily every custom field on the screen. A meta-box save handler hooked to save_post that unconditionally reads $_POST[‘my_field’] and writes it straight to post meta will see an empty or missing value during that autosave request and happily overwrite the real, previously saved value with nothing.

🔎 The Problem

add_action('save_post', function ($post_id) {
    // Runs on every save_post call, including autosave requests,
    // where $_POST['event_location'] simply isn't present.
    update_post_meta($post_id, 'event_location', $_POST['event_location'] ?? '');
    // Result: every autosave quietly blanks out event_location.
});

✅ Fix: Bail Out of Autosave and Ajax Requests Explicitly

  • At the top of the save_post handler, return early if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) so a background autosave never gets the chance to touch real post meta.
  • Verify a nonce specific to your meta box before writing anything, and only write a field’s meta if that field’s key is actually present in $_POST, rather than defaulting a missing key to an empty string.
  • If the field must stay in sync even during autosave, store it in the revision’s own meta (which WordPress supports per-revision) instead of overwriting the parent post’s meta from an autosave request.

🚨 Why This Is Dangerous

  • A field that resets every 60 seconds will eventually catch an editor mid-typing between saves, so the data loss looks random and intermittent instead of pointing straight at the autosave cycle that’s actually causing it.

An autosave is supposed to protect your work, not quietly stand in for it — a meta-box handler that can’t tell the two apart will keep erasing exactly the field it was meant to preserve.

— WordPress Admin

Related posts:

WordPress: Fix the Media Library Showing Broken Thumbnails After a Migration Because the Image Sizes...

WordPress: Fix the Media Library Showing Broken Thumbnails After a Migration Because the Image Sizes...

WordPress: Organize Media Library with Folders (Plugins)

WordPress: Organize Media Library with Folders (Plugins)

Wordpress: Boosting API Performance with the Transients API

WordPress: Boosting API Performance with the Transients API

Views: 4

Post navigation

Docker: Fix a Healthcheck Marking a Container Unhealthy Too Soon
HTML5: Fix a datalist Suggestion List Not Filtering as You Type

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,056)
  • Get the User Name and Domain Name from an Email Address in SQL (982)
  • How to add default value for Entity Framework migrations for DateTime and Bool (980)
  • Get the First and Last Word from a String or Sentence in SQL (859)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (657)
  • Add Constraint to SQL Table to ensure email contains @ (612)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (548)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • HTML5: Fix a datalist Suggestion List Not Filtering as You Type
  • WordPress: Fix a Custom Field Resetting After Every Autosave
  • Docker: Fix a Healthcheck Marking a Container Unhealthy Too Soon
  • Windows 11: Fix Voice Typing Placing Punctuation in the Wrong Spot
  • Git: Fix a Stash Pop Conflicting With Unrelated Untracked Files
  • Photoshop: Fix the History Brush Reverting Past a Resize Step
  • SQL Server: Fix a View Returning Stale Data After a Schema Change
  • CSS: Fix an aspect-ratio Box Collapsing When Content Overflows
  • Photoshop: Fix the Color Picker Clamping a Custom Swatch’s Hue
  • JavaScript: Fix Array.sort Reordering Equal Items Unexpectedly

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,056)
  • Get the User Name and Domain Name from an Email Address in SQL (982)
  • How to add default value for Entity Framework migrations for DateTime and Bool (980)
  • Get the First and Last Word from a String or Sentence in SQL (859)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • HTML5: Fix a datalist Suggestion List Not Filtering as You Type
  • WordPress: Fix a Custom Field Resetting After Every Autosave
  • Docker: Fix a Healthcheck Marking a Container Unhealthy Too Soon
  • Windows 11: Fix Voice Typing Placing Punctuation in the Wrong Spot
  • Git: Fix a Stash Pop Conflicting With Unrelated Untracked Files

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com