I have been developing a plugin that creates a custom post type. The custom post type has many custom fields and I want to set default some values for these custom fields on activation.

How can I do this?

<li>
<label for="cx_number" class="sinop">Post Limit</label>
<input style="width:50px;" type="number" name="cx_number" id="cx_number" value="<?php if( !empty ( $postData['cx_number']) ) echo $postData['cx_number'][0]; ?>"/>

if ( isset( $_POST[ 'cx_number' ] ) ) {
    update_post_meta( $post_id, 'cx_number', $_POST[ 'cx_number' ] ) ;
}

1 Answer
1

you can do that on a save_post_ hook
try that :

add_action("save_post_" . CUSTOM_POST_TYPE, function ($post_ID, \WP_Post $post, $update) {

    if (!$update) {

        update_post_meta($post->ID, "cx_number", "default value");

        return;

    }


    if (isset($_POST["cx_number"])) {

        update_post_meta($post->ID, "cx_number", $_POST["cx_number"]);

    }


}, 10, 3);

Leave a Reply

Your email address will not be published. Required fields are marked *