-1

I have a post type in which I need to save price with each item.

Please, suggest how can I create meta-field in my post type, without using any plugin?

closed as too broad by Michael, kaiser Jan 9 '18 at 19:34

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

0

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');
?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.