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

Save meta data with post, Without using any plugin [closed]

You can add custom meta fields for doing so.

Here, is sample code for adding a meta field for post type post. For more details please refer to link.

<?php

    /************************************************************************/
    function myPrice_meta_box()
    {
        add_meta_box('myPrice_field', 'Price', 'myPrice_meta_box_callback', 'post', 'normal', 'high');
    }

    add_action('add_meta_boxes', 'myPrice_meta_box');

    /**
     * Prints the box content.
     *
     * @param WP_Post $post The object for the current post/page.
     */
    function myPrice_meta_box_callback($post)
    {
        global $post;
        // Add a nonce field so we can check for it later.
        wp_nonce_field('myPrice_meta_box', 'myPrice_meta_box_nonce');
        /*
        * Use get_post_meta() to retrieve an existing value
        * from the database and use the value for the form.
        */
        $item_Price = get_post_meta($post->ID, '_price_', true);
    ?>
            <p class="order_id">
                <span>
                    <label>Price::</label> 
                    <input type="text" name="_price_" size="50" value="<?php echo $item_Price ?>" placeholder="Price">
                </span><br/>
            </p>
    <?php
    }

    /**
     * When the post is saved, saves our myPrice data.
     * @param int $post_id The ID of the post being saved.
     */
    function myPrice_meta_box_data($post_id)
    {   
        /*
        * We need to verify this came from our screen and with proper authorization,
        * because the save_post action can be triggered at other times.
        */
        /* Check if our nonce is set.*/
        if (!isset($_POST['myPrice_meta_box_nonce']))
        {
            return;
        }
        /* Verify that the nonce is valid.*/
        if (!wp_verify_nonce($_POST['myPrice_meta_box_nonce'], 'myPrice_meta_box'))
        {
            return;
        }

        /* If this is an autosave, our form has not been submitted, so we don't want to do anything.*/
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        {
            return;
        }

        $price = isset($_POST["_price_"] ) ? $_POST["_price_"]  : '';

        /* Update the meta field in the database.*/
        update_post_meta($post_id, '_price_', $price);
    }

    add_action('save_post', 'myPrice_meta_box_data');
?>

Related Posts:

  1. Passing error/warning messages from a meta box to “admin_notices”
  2. Metabox of one post influence setting on other
  3. unable to save post meta on single field with multiple selects
  4. get_post_meta as a list for drop down search filter
  5. Editor meta box Showing but not saving
  6. save radio button selection in post-meta on submit
  7. Add “upload media” button in meta box field
  8. Metabox with checkbox is not updating
  9. get registered metaboxes by post type or post ID
  10. How to get meta box data to display on a page
  11. esc before saving or before displaying does it matter?
  12. Add a meta description to home page?
  13. Why can’t I hook into save_posts after admin_init?
  14. Check if meta key value already exists
  15. get_post_meta doesn’t work
  16. Save checkboxes of a metabox with a foreach loop (invalid argument)
  17. prevent post submission
  18. How to store multiple input values with same meta_key
  19. Consolidate Metaboxes into 1 Big Metabox
  20. metabox upload file
  21. Why is variable value emptry in $_POST but available in $_REQUEST?
  22. Displaying Metabox value (custom post type taxonomy)
  23. Why does get_post_meta not work with the posts page?
  24. How to get meta box values – WP tuts tutorial
  25. Change the post date from a meta box
  26. Undefined ‘post_type’ error on Add new page
  27. update_post_meta and get_post_meta not working
  28. Repeatable custom meta fields
  29. Metabox saves on Update or Publish, but not on Saving Draft
  30. how to save multiple checkboxes value in wordpress dynamically
  31. Using WYSIWYG In custom meta boxes
  32. save_post action to include wp_insert_post_data filter, gathering meta field info & prevent infinite loop
  33. Multiplicate entry on update_post_meta
  34. Meta box html is not updating after saving
  35. Getting gutenberg (WordPress 5) to save TInymce data?
  36. Undefined index error in custom post metabox
  37. Custom Meta box only returns most recent value on page
  38. How to prevent further updates of custom meta when using actions to set one meta based on another
  39. Stop saving process when metabox is invalid [duplicate]
  40. How to create a button click counter meta box?
  41. meta box & callback function
  42. $_GET & $post_ID
  43. Why does not my metabox save?
  44. How to validate select field in post meta?
  45. Migrating from metaboxes to wp_editor()
  46. Parse a text area custom meta box and assign as value to existing meta keys
  47. Getting metabox value?
  48. Hide Page Title with Post Meta
  49. How many meta-query fields support WordPress search at a time?
  50. How to check specific value in two metabox?
  51. How can i get multi checkboxes value in metbox?
  52. WP_Meta_Query object with conditionals
  53. Show value of select dropdown in meta box
  54. remove a single post_meta
  55. Echo 2 values from one key in Array with get_post_custom
  56. post meta – problem : copy the same meta for all the articles
  57. Custom Meta Data is not being saved
  58. How using the Meta Box plugin, to filter posts by the value of a post type field?
  59. I can not display meta value in extras.php and template-tags.php
  60. Post and Pages section inside WordPress admin are completely blank
  61. How to improve my non-unique metadata MySQL entries?
  62. Saving multiple fields (dropdown and text) in custom metabox
  63. Update Post Meta in Front End with a form
  64. Update Post meta with custom variable
  65. get_post_meta not working on my custom-function page
  66. update_post_meta doesn’t work
  67. Custom meta box data not saving
  68. Saving custom fields for WP_Query to retrieve
  69. How to show all the associated posts with specific date of data metabox?
  70. Removing Meta Generator
  71. Meta Box – Javascript Datatable
  72. Only allow one meta key value per post in a category
  73. use post meta in add_meta_boxes action
  74. Problem with ‘save_post’ hook not running
  75. A correct hook for saving meta boxes data
  76. Custom meta box not saving values of radio buttons in WordPress
  77. Can’t save drop down select date in meta boxes
  78. update_post_meta does not work
  79. add_post_meta not saved
  80. when saveing $meta_box i get Undefined index error
  81. Updating post meta and Meta Box plugin
  82. Related posts and custom meta_box?
  83. Select Options Meta Data is Not Updating in Edit Meta Box
  84. Metaboxes not saving data
  85. How to display meta box data using “Meta boxes as needed”
  86. Metadata in loops
  87. Allow iframe in custom meta box
  88. Calling Data from Custom Meta Box
  89. How can i remove blank area caused by theme’s post meta boxes?
  90. How we insert values into database using metabox WordPress?
  91. Datepicker altField and altFormat to save a new meta key/value in a post?
  92. My meta box don’t want to save value
  93. CPT posts in drop downed in meta box on page doesn’t return post ID
  94. Create more Meta Boxes as needed
  95. meta_query with meta values as serialize arrays
  96. Set Default Admin Screen options & Metabox Order
  97. How to set default screen options?
  98. Add a Meta Box for uploading a SECOND Featured Image?
  99. Does WordPress have a “Form API”?
  100. How to HIDE everything in PUBLISH metabox except Move to Trash & PUBLISH button
Categories metabox Tags meta-query, metabox, post-meta, save-post
WordPress url transformation script
WordPress media library allow uploading fake file

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