<?php
function auto_set_category( $post_id ) {
$post = get_post( $post_id );
$title = $post->post_title;
$categories = array();
$category = 'Health';
if ( stripos( $title, $category ) !== false
|| stripos( $title, 'hospital' ) !== false
|| stripos( $title, 'disease' ) !== false ) {
array_push($categories, $category);
}
$category = 'Environment';
if ( stripos( $title, $category ) !== false
|| stripos( $title, 'nature' ) !== false
|| stripos( $title, 'forest' ) !== false ) {
array_push($categories, $category);
}
$category = 'Education';
if ( stripos( $title, $category ) !== false
|| stripos( $title, 'school' ) !== false
|| stripos( $title, 'student' ) !== false ) {
array_push($categories, $category);
}
if( count($categories) === 0) {
array_push($categories, 'News');
}
wp_set_object_terms( $post_id, $categories, 'category' );
}
add_action( 'save_post', 'auto_set_category' );
add_action( 'publish_post', 'auto_set_category' );