auto check a category when creating new post code example
Example: auto check a category when creating new post
add_filter('wp_terms_checklist_args', 'WPSE_pre_select_categories', 10, 2);
function WPSE_pre_select_categories($args, $post_id) {
$post = get_post($post_id);
if ($post->post_status !== 'auto-draft' || $post->post_type !== 'post')
return $args;
$select_categories = [4, 6];
if (empty($args['selected_cats'])) {
$args['selected_cats'] = [];
}
$args['selected_cats'] = array_unique(array_merge($args['selected_cats'], $select_categories));
return $args;
}