Setting a post type to be enabled as an event by default

The below snippet will ensure every post of type 'post' has the _piecal_is_event option turned on when saved. Note that this should only be used if you always want posts of that type to be an event, as you won't be able to turn off the _piecal_is_event option once this snipped is enabled.

function piecal_is_event_by_default($post_id) {
    // Avoid recursion or unnecessary actions
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    // Check if it's the correct post type
    $post_type = get_post_type($post_id);
    if ($post_type !== 'post') { // Change the post type to the slug of your Events CPT
        return;
	}

    // Set the default value if the field is not set
    if (!get_post_meta($post_id, '_piecal_is_event', true)) {
        update_post_meta($post_id, '_piecal_is_event', '1');
    }
}
add_action('save_post', 'piecal_is_event_by_default');
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.