can you set a default value to a custom field

You can check it by adding a hook to save_post action. In this way all your posts will have default value for a custom field.

add_action( 'save_post', 'wpse8170_save_post', 10, 2 );
function wpse8170_save_post( $post_id, WP_Post $post ) {
    if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || !current_user_can( 'edit_post' ) ) {
        return;
    }

    if ( has_category( 22, $post_id ) && !get_post_meta( $post_id, 'my-custom-field', true ) ) {
        update_post_meta( $post_id, 'my-custom-field', 'my default value' );
    }
}

Leave a Comment