By kafir i mean person who does shirk , like hindu and buddhist and the food was halal just he touched his wet hands on that food and was not wearing gloves.

Ok so the food was Najas. The narration you mentioned does not apply for all good deeds but specifically mentions prayers not being accepted for 40 days. This means one does not receive the thawab (reward) of their prayers during this time, although they still make them to fulfill their obligations. As for Ziyarat for every step you take towards this your sins are forgiven, so youre prayers would be answered inshallah