Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Custom Post type forgetting data

In the function hooked onto save_posts you should make sure that you check that the action wasn’t triggered by an auto-save routine. I suspect that the reason why the post ‘forgets’ the data is that the post auto-saves, and updates the post-meta with blank data.

To do this:

function save_details($post_id){
    //Make sure you check this isn't an autosave.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
         return;

    update_post_meta($post_id, "media_meta", $_POST["media_meta"]);
    update_post_meta($post_id, "highlights_meta", $_POST["highlights_meta"]);
    update_post_meta($post_id, "main_meta", $_POST["main_meta"]);
    update_post_meta($post_id, "tabbed_meta", $_POST["tabbed_meta"]);
}

I’ve had a quick glance at the tutorial. It seems to ignore best practice and security checks such as the following:

  1. The function hooked on save_posts is given the argument $post_id
    (the post ID). You should use this rather than global $post.
  2. Prefix your functions. save_details is very generic, and if the WordPress core or another plug-in uses that function name, it’ll crash your blog. This is particularly important for plugin developers. Prefix it with something unique: my_name_save_details. Same goes for the other functions.
  3. Check user permissions. Use current_user_can to verify that a user is allowed to be editing the post.
  4. Uses nonces. In your metabox, add a nonce and verify it in my_name_save_details. This checks the data you’re adding to your database really came from your metabox.(See Codex)
  5. Perform other checks. save_posts fires every time a post or page is saved/updated, but you only really want to be altering posts of your custom post type. It’s not essential, but removes the possibility of upsets further down the line. You can use get_post_type to help with this.

Related Posts:

  1. is there an update_post_meta action
  2. WooCommerce wp_insert_post action firing on every save
  3. remove_action or remove_filter with external classes?
  4. add_action reference a class
  5. Why does save_post action fire when creating a new post?
  6. Remove parent theme action in child
  7. How to know what priority to use with add_action()?
  8. How to do_action and get a return value?
  9. Remove an action from an external Class
  10. Difference between do_action_ref_array() and do_action()
  11. Can my “add_action” function know the name of the hook calling it?
  12. Why is there both a save_post and wp_insert_post action?
  13. Does the event ‘wp_version_check’ even exist? What is it doing?
  14. When is admin_init Action ran?
  15. Can an action callback prevent the parent from continuing execution?
  16. remove_action in a theme
  17. add_action ‘manage_posts_custom_column’ in a class [closed]
  18. How do I enqueue a script to run inside the Gutenberg editor?
  19. How can I find out what functions are assigned to actions?
  20. Unable to prevent function using save_post firing twice
  21. Difference between do_action(‘admin_enqueue_scripts’, $hook_suffix) and do_action(“admin_print_styles-$hook_suffix”) syntax
  22. Cron jobs in a class
  23. Add something to beginning of the content
  24. Disable WooCommerce action
  25. Content hooks vs User hooks
  26. How can I remove the WP menu from the admin bar?
  27. When and Where to use wp_insert_post()
  28. Why, Where, and When to use reference pointers in filters/hooks?
  29. Restrict post edit/delete based on user ID and custom field
  30. add_action customize_register not working
  31. Passing arguments into ‘init’ function
  32. Add a Custom Field in Comment Box AFTER text area BUT BEFORE Send button
  33. How to use conditional add_action for WordPress
  34. Using wp_insert_post and post_update_meta but need to fire save_post afterward
  35. WP Schedule Event – Every Day When First Visitor Comes
  36. enqueuing React script and hooking its target div fails to load script
  37. Change meta tags programatically
  38. wp_logout action not working
  39. How to remove an action that is added inside a class
  40. Having an add_action( ‘user_new_form’,) [closed]
  41. Using auth_redirect returns cannot modify header information
  42. add action which returns modified value
  43. What would cause the ‘wp’ action to fire twice per page (but only once per post) in Firefox only?
  44. Problem:Save Several Duplicate posts in The Database and then Error nesting level of ‘100’ reached
  45. How to remove an action added by a child theme of Genesis
  46. I don’t understand why I need a lower priority to remove an action with a higher priority to make it work
  47. WordPress admin WP_table_list show incorrectly
  48. wp_get_attachment_metadata returns false with add_action() ‘add_attachment’ hook
  49. How to add a checkbox inside the “Publish post” widget?
  50. Manually add admin bar
  51. Is there a way to expose additional fields to the Bulk Action > Edit functionality?
  52. What’s the usage of action do_meta_boxes?
  53. How can I invoke an action after a CiviCRM triggering event?
  54. Custom bulk actions in WP 3.7.1
  55. Remove action from a plugin class
  56. do_action pass array argument as reference not copy [duplicate]
  57. how can I use add_action with external class which the function contain 2 argument?
  58. remove_action not working with procedural function
  59. Why Should We Use wp_clear_scheduled_hook and What it Does?
  60. Woocommerce Change placement with wrapper
  61. Modify query in pre_get_posts action is messing up my nav menu
  62. Exclude trash from save_post
  63. How to check if an action is already fired?
  64. add_action and wp_head not display content in head section
  65. Scheduled event won’t fire
  66. Get updated post meta on save_post action?
  67. Which action fire in front-end single post only
  68. When should I call add_settings_section and add_settings_field?
  69. update_post_meta() whenever custom post type is updated
  70. use add_action(‘wp_head’) in a widget
  71. Explanation for remove_filter used in the below code [closed]
  72. Unable to show error message using wp_login action
  73. Usage of do_action and add_adction
  74. array of objects to do_action, callback gets singular object?
  75. Preventing frontpage to be deleted/moved to trash
  76. Filter list by a unique meta value dilemma
  77. Post IDs missing on delete_postmeta action hook
  78. WP action is not triggering a do_action() call within?
  79. Selectively applying action based on role
  80. Mustn’t do_action be accompanied with a function?
  81. where is the documentation for add_action() parameters?
  82. How to set a different value in the generator meta tag?
  83. How to remove a custom action on child theme?
  84. Action hook on only publishing post not on editing or updating
  85. how can I use add_action run another function when publishing new post?
  86. Call add_action() in function wordpress
  87. How to generate numbers indistinguishable for the IDs of the posts
  88. How to remove function?
  89. Performance : Duplicating `add_action`
  90. Where to hook my plugin’s action
  91. Validate form in functions.php, send errors back to template
  92. Set default Custom Post Meta Value
  93. What is the callback sent to add_submenu_page() invoked by WordPress?
  94. remove_action returns FALSE
  95. add_action in class and use it in theme
  96. Remove action from shortcode
  97. How to track post number on the_loop
  98. How can I trigger actions manually?
  99. Write automatic title at save_post (infinite loop)
  100. How to get $tag to fill into add_action() or add_filter()?
Categories actions Tags actions, post-meta
Problem on the_excerpt(); function
Help solving t_echo php error while using echo do_shortcode

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress