I am a novice and not that familiar with PHP. I tried for some hours to bring your code to work. I believe that it has an error. I printed out some information and got the feeling that the hook is at the wrong place. When it is invoked the post seems to be not yet activated. I printed out the post-type and the tax an both where empty. By the way – there seems to be a coding error at the end of the snippet.
Do you have an idea how the code could work correct and which hook should be used.