In WordPress, you can organise the category with a relationship parent/child. This means that a single post belongs to multiple categories. Let’s see how to get the parent category of a post:
function get_parent_category( $post_id ) {
$cats = wp_get_object_terms($post_id, 'category', ['fields' => 'all'] );
foreach( $cats as $key => $cat ) {
if( $cat->parent === 0) {
return $cat;
}
}
return null;
}