As usual with multiple loops, one of which is main loop, the issue is messy.
What it looks like is happening:
- Secondary loop runs and deals with some posts.
- Main loop runs and deals with some posts.
What is actually happening:
- WordPress core is loaded.
- Posts are queried for main loop.
- Template file is decided on and loaded.
- Secondary loop runs and deals with some posts.
- Main loop runs and outputs those posts queried way above in the process.
To cleanly accomplish what you want would take:
- completely moving that featured logic out of template
- using it to override what is queried for main loop on
pre_get_posts
hook (massive topic by itself, search around the site) - storing that info and using it way later to run custom loop in template
It’s not overly complicated, but very menial and prone to errors (as in I wouldn’t even try to modify this going by snippets without hands on access and testing on actual theme and site).