2

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

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);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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