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

How we insert values into database using metabox WordPress?

The following example is adding a meta box to the post edit screen and the wporg_cpt edit screen.

 function wporg_add_custom_box()
  {
     $screens = ['post', 'wporg_cpt'];
     foreach ($screens as $screen) {
        add_meta_box(
        'wporg_box_id',           // Unique ID
        'Custom Meta Box Title',  // Box title
        'wporg_custom_box_html',  // Content callback, must be of type callable
         $screen                   // Post type
         );
        }
    }
    add_action('add_meta_boxes', 'wporg_add_custom_box');

The wporg_custom_box_html function will hold the HTML for the meta box.

The following example is adding form elements, labels, and other HTML elements.

function wporg_custom_box_html($post)
{
    ?>
    <label for="wporg_field">Description for this field</label>
    <select name="wporg_field" id="wporg_field" class="postbox">
        <option value="">Select something...</option>
        <option value="something">Something</option>
        <option value="else">Else</option>
    </select>
    <?php
}

Saving Values
The following example will save the wporg_field field value in the _wporg_meta_key meta key, which is hidden.

function wporg_save_postdata($post_id)
{
    if (array_key_exists('wporg_field', $_POST)) {
        update_post_meta(
            $post_id,
            '_wporg_meta_key',
            $_POST['wporg_field']
        );
    }
}
add_action('save_post', 'wporg_save_postdata');

follow https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/

Related Posts:

  1. I can not display meta value in extras.php and template-tags.php
  2. post formats – how to switch meta boxes when changing format?
  3. Meta compare with date (stored as string) not working
  4. Adding an assisting editor box to Post page
  5. How metadata API works?
  6. Metabox Data not being saved [closed]
  7. Displaying Metabox value (custom post type taxonomy)
  8. Options to get my custom post type metadata via the WordPress API
  9. How to stop wp_postmeta from being called on archive and search pages?
  10. WP_POSTMETA – What do these values mean inside the data structure?
  11. How to create a meta_query to get all posts with a specific meta data?
  12. Compare meta_query with a Regular Expression and do a less-than operation on it
  13. $wpdb class updating meta_value using Ajax [closed]
  14. Should I save this mulit dementional arary as one post meta?
  15. How to check if a post meta key/value pair already exists for a specific post
  16. get_post_meta as a list for drop down search filter
  17. Add box with custom per-page properties
  18. Save meta data with post, Without using any plugin [closed]
  19. WP_Meta_Query object with conditionals
  20. get Insert id for meta field
  21. add post meta front end edit
  22. Filter posts by meta key
  23. Search post overlapping dates – meta_query with meta_key
  24. How can I query for posts using a date stored in post-meta?
  25. Negative meta_query if storing multiple post_meta values with shared meta_key
  26. How to improve my non-unique metadata MySQL entries?
  27. How to get posts by meta value as multi-dimensional array?
  28. Add post meta data date to event
  29. Check if any meta on the post has value then display content
  30. Combine meta query and give a specific meta query a higher priority
  31. Get or set values in post meta
  32. I created a Custom Meta Box but it is not displaying the value on my post page
  33. update_post_meta() throws Uncaught error: Cannot create duplicate attribute
  34. Optimize WP Meta Query for large amount of post meta?
  35. Multiple meta key and value search in the query
  36. Saving custom fields for WP_Query to retrieve
  37. How to show all the associated posts with specific date of data metabox?
  38. Multiple postmeta values to the same post_id/meta_key combination?
  39. Retrieve posts from meta key
  40. How to show specific meta keys of all posts in admin panel?
  41. How to create a link for wordpress meta datas?
  42. Remove action of an external plugin after checking if custom post meta is set
  43. Post meta box data not saving
  44. Filtering Tabs in a Custom Post Type’s Edit Page
  45. Hide individual page title using checkbox in custom meta box?
  46. How do I retrieve the slug of the current page?
  47. meta_query with meta values as serialize arrays
  48. Most efficient way to get posts with postmeta
  49. Get posts by meta value
  50. Explanation of update_post_(meta/term)_cache
  51. How to extract data from a post meta serialized array?
  52. Passing error/warning messages from a meta box to “admin_notices”
  53. Add “upload media” button in meta box field
  54. How to save an array with one metakey in postmeta?
  55. WordPress is stripping escape backslashes from JSON strings in post_meta
  56. How can I get the post ID from a WP_Query loop?
  57. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  58. Use REGEXP in WP_Query meta_query key
  59. What is the index [0] for on post meta fields?
  60. How to update_post_meta value as array
  61. Adding meta tag without plugin
  62. What’s the point of get_post_meta’s $single param?
  63. What is the different between an attachment in wp_posts and an attachment in wp_postmeta?
  64. How to edit a post meta data in a Gutenberg Block?
  65. Sanitizing integer input for update_post_meta
  66. Execute action after post is saved with all related post_meta records (data)
  67. Lack of composite indexes for meta tables
  68. How can I retrieve multiple get_post_meta values efficiently?
  69. Get a single post by a unique meta value
  70. if get_post_meta is empty do something
  71. How we get_post_meta without post id
  72. How get post id from meta value
  73. What is the code to get the download link for a product in WooCommerce?
  74. Safe to delete blank postmeta?
  75. Custom field metabox not showing in back-end
  76. advanced custom fields update_field for field type: Taxonomy
  77. WP_Query with checkbox meta_query
  78. update_post_meta not saving when value is zero
  79. Content hooks vs User hooks
  80. Getting attachments by meta value
  81. How to hide meta box values from custom fields list?
  82. Ordering posts having multiple post-meta date fields
  83. Trying to get custom post meta through Jetpack JSON API [closed]
  84. How to update/insert custom field(post meta) data with wordpress REST API?
  85. get registered metaboxes by post type or post ID
  86. How do I remove all the metaboxes for a custom post type?
  87. Restrict post edit/delete based on user ID and custom field
  88. Custom Queries: Joining On Meta Values From Two Custom Post Types
  89. get_post_meta returning empty string when data shows in the database
  90. publish_post action hook doesn’t give post_meta_data
  91. Remove WordPress.org Meta link
  92. Remove post meta keys
  93. How to access the post meta of a post that has just been published?
  94. Why time functions show invalid time zone when using ‘c’ time format?
  95. How to get meta box data to display on a page
  96. Save metabox with multiple checkbox array
  97. esc before saving or before displaying does it matter?
  98. Why is get_post_meta returning an array when I specify it as single?
  99. How to batch update post content with custom post meta value
  100. How Can I save multiple records in same meta key?
Categories post-meta Tags meta-query, metabox, post-meta
I need css code to divide my webpage sections into two columns
Bulk update custom post types

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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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