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 field not updating when value is empty

You are checking if the field is empty. Try to update it if its empty as well

 if( trim( $attachment[ $field ] ) == '' ) $post[ 'errors' ][ $field ][ 'errors' ][] = __( 'Error! Something went wrong.' );
 else update_post_meta( $post[ 'ID' ], $key, $attachment[ $field ] );

Try

if( isset( $attachment[ $field ] ) ) {
            if( trim( $attachment[ $field ] ) == '' )
            $post[ 'errors' ][ $field ][ 'errors' ][] = __( 'Error! Something went wrong.' );
            endif;
            update_post_meta( $post[ 'ID' ], $key, $attachment[ $field ] );
        } 

Related Posts:

  1. Display info from custom fields in all images’ HTML
  2. copy attachments to another post type and change attachment url
  3. ajax delete value from custom field array
  4. Save attachment custom fields on front end
  5. Media Attachment Custom Meta Fields not saving in Media Uploader when using jQuery UI Autocomplete
  6. Can I exclude a post by meta key using pre_get_posts function?
  7. Custom post meta field effect on the performance on the post
  8. How to get custom post meta using REST API
  9. Difference between meta keys with _ and without _ [duplicate]
  10. Remove Dimension from wp_get_attachment_image
  11. Orderby meta_value only returns posts that have existing meta_key
  12. What is the index [0] for on post meta fields?
  13. What is “meta_input” parameter in wp_insert_post() used for?
  14. How to enable revisions for post meta data?
  15. The “_encloseme” Meta-Key Conundrum
  16. Best way to programmatically remove a category/term from a post
  17. Using get_post_meta with new_to_publish
  18. Custom field metabox not showing in back-end
  19. So much data in postmeta
  20. explode array within shortcode
  21. Can I count the number of users matching a value in a multiple value key?
  22. How can I append custom data to images in the editor?
  23. Getting attachments by meta value
  24. When using add_post_meta and update_post_meta, is there any way to give the individual arrays keys?
  25. How to hide meta box values from custom fields list?
  26. Auto sort the wp-admin post list by a meta key
  27. get_post_meta() unserialize issue – returns boolean(false)
  28. What is the advantage of the wp_options design pattern?
  29. How can I remove fields in the attachment editor?
  30. Storing meta fields multiple times OR once with multi dimensional array?
  31. Allow user to create instances of custom field
  32. display specific custom fields
  33. Is there a hook / action that is triggered when adding or removing a post thumbnail?
  34. Saving Custom Field in Attachment Window in WordPress 3.5
  35. Add multiple images to a page sidebar
  36. Meta keywords and descriptions plugin for manually editing meta for each page/post
  37. passing argument to get_template_part() or a better way to code
  38. Is it possible to store arrays in a custom field?
  39. Get updated meta data after save_post hook
  40. Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
  41. Save HTML formatted data to post meta using add_post_meta()
  42. How to set a default meta value for custom field
  43. importing data from non-wordpress mysql db
  44. Add custom field to image editor
  45. Create meta boxes that don’t show in custom fields
  46. Transients vs CRON +Custom Fields: Caching Data Per Post
  47. Unable to save datetime custom meta field using update_post_meta() function
  48. Up/Down voting system for WordPress
  49. How to save custom fields for attachments
  50. post meta data clearing on autosave
  51. Create custom field on post draft or publish?
  52. Ordering posts by anniversary using only day and month
  53. get_post_meta fields don’t show up on posts page
  54. Update meta values with AJAX
  55. How to break meta values into different items and avoid duplicates?
  56. Cannot edit post meta fields with rest API
  57. How to use pagination with get_post_meta
  58. Copying Custom Meta Values from existing post to a duplicate post
  59. How to get custom field image url of specific size
  60. Add a post meta key and value only if it does not exist on the post
  61. Move value of one custom field to another
  62. Order posts according to user defined order for meta values?
  63. Displaying posts with only upcoming dates according their custom field date value
  64. Custom fields to save multiple values
  65. Custom fields: In what order are they saved into the DB?
  66. Function to change meta value in database for each post
  67. Get a post_id where meta_value equals something in a serialized meta_value field
  68. Get aggregate list of all custom fields for entire blog
  69. Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
  70. Unable to show ACF’s Image Custom Field properly in Genesis Framework [closed]
  71. Custom field value based on other custom field values
  72. wp_handle_upload error “Specified file failed upload test” but still creates attachment?
  73. How to save a ToggleControl value in a meta field?
  74. Which is best in the following scenario : post_meta vs custom table vs parent/child posts
  75. Saving custom image meta fields
  76. Author Page Custom Query WHERE author OR [post meta value] OR [post meta value]
  77. How to display Meta Field Value?
  78. MySQL query to set wp_postmeta using term_taxonomy_id value
  79. Insert attachment ID in custom field from media uploader
  80. How to Validate Post Meta type/extension (Video File Image File etc)
  81. Get all meta keys assigned to a post type
  82. using multiple meta_key and meta_value in query_posts
  83. Custom Meta Box not Saving in Posts with Gutenberg Editor
  84. Adding custom fields (post meta) before/during wp_insert_post()
  85. Combine multiple custom field values into single value
  86. How can I sort homepage by a meta value?
  87. Get specific custom field keys from a post and put into an array
  88. How do I use wp_query for WordPress search?
  89. Nav Menu – Add class based on meta keys
  90. How to query posts with certain custom meta data, and output Post data
  91. MySQL Query that looks for post with Custom Field, then changes Category
  92. ACF: How to get the full field name (meta_key) by a field key?
  93. How can I store a file in the database in the same way WordPress and ACF do?
  94. How to wrap meta values seperated by comma in ? [closed]
  95. Bulk remove post meta
  96. post meta getting deleted on save
  97. How to create html block to display extra information on woocommerce single product page
  98. Can’t get post ID using wp_insert_post_data
  99. Unique Post Meta Values
  100. Create Multiple File Upload Metabox in WordPress
Categories custom-field Tags attachments, custom-field, post-meta
Comments vs. Pingbacks next page issue
How to remove attachments size attribute [duplicate]

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