WordPress custom Query for Posts in Category display posts multiple times

Don’t use query_posts() – it breaks things.

There’s no need to run a query for each category when you can create a single query to snag posts in any specified category simultaneously. This is both more efficient as well as a means to see you don’t end up with duplicates.

Since you were using query_posts() to replace the main query, the correct solution then is to modify the main query in a pre_get_posts action hook – or completely replace it with a WP_Query instance of your own design. In particular, you’ll need to use the category parameters – most likely category__in which allows you to specify an array of category ids.

All that in mind, your code doesn’t make much sense – it obtains a list of every category, then queries the same category 0 as many times as there different categories. I.e., if there are 6 different categories, then that code queries category 0 for posts 6 times instead of querying each unique category.

Further, if you want to query every existing category for posts, there’s no reason to include category query arguments at all – in omitting category query arguments, WordPress will perform a query for posts in any category.