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. Hide individual page title using checkbox in custom meta box?
  45. Add “upload media” button in meta box field
  46. How get post id from meta value
  47. Safe to delete blank postmeta?
  48. get_post_meta returning empty string when data shows in the database
  49. publish_post action hook doesn’t give post_meta_data
  50. How to access the post meta of a post that has just been published?
  51. Query between two meta values?
  52. List posts under meta_value heading
  53. get_post_meta doesn’t work
  54. WP Meta Query for some meta (array) values
  55. How to display liked posts of current user in wordpress?
  56. loop through all meta keys with get_post_meta
  57. Change the post date from a meta box
  58. Saving html into postmeta without stripping tags – safe?
  59. update_post_meta and get_post_meta not working
  60. wp_update_user isn’t instantly?
  61. Searching by meta values showing inappropriate result
  62. Repeatable custom meta fields
  63. Performance when getting post meta for post retrieved by meta value
  64. How to use update_post_meta inside wp_trash_post
  65. “update_post_meta” not working in “wp_insert_post_data” hook
  66. Why is my Custom Meta Box Field Inputs NOT saving?
  67. hide posts with specific meta data from admin page
  68. Custom RSS Feeds & Post Meta Data
  69. New WP_Query loop in admin causes problems
  70. Is it possible to retrieve a post and its metadata at the same time?
  71. How can you include custom post meta in search without calling each key?
  72. cleaning up safely wordpress wp_postmeta table
  73. Post meta as array looks like string instead of array
  74. Why does get_transient() always return string even if integer set?
  75. Author_meta ONLY if it exists
  76. Is there a way to disable post meta caching for development?
  77. Post meta select input, if statement
  78. update_post_meta not working?
  79. Drag and Drop Metadata Menu Order
  80. delete duplicates wp_postmeta
  81. Post Thumbnail missing when using webp format while sharing
  82. Bulk Update Post Meta Values from Different Post via Rest Api
  83. update_post_meta() is not saving the value
  84. Only show meta on one post type on search results page
  85. Get post content before rendering
  86. wp_insert_post inside save_post adds wrong metadata to inserted post
  87. Rename image filename using ‘media_handle_upload’
  88. Using Self Hosted Video URL With Custom Fields
  89. get_post_meta not work in php foreach
  90. Why are my custom metaboxes not updating the post meta?
  91. Performace on 1 million plus meta fields vs 1 field with 1 million multi array
  92. Return a single custom post from multiple meta queries
  93. how to echo/display the custom field value in specific custom field name?
  94. update_post_meta() not working in bulk option
  95. How to update the ‘modified_time’ of a post ONLY when content is changed?
  96. Multiple If else statements on Search Result Page
  97. get_children filter with postmeta
  98. wordpress query making site very slow
  99. if condition from post_meta not working in save_post
  100. How to append $metadata[‘sizes’] to metadata when using ‘wp_generate_attachment_metadata?
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
  • 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