added error handling and get_post_type