The Primary Category feature comes from Yoast SEO.
That code they provided is to display the primary category titles on a page. It’s not meant to change the output of your archives to only show primary categories.
The pre_get_posts action might be a good starting point, but I’m not familiar enough with Yoast to know the exact code.
It may be worth checking with their support to see if it’s something they’ve helped with before.