Warehouse clubs are their own category
Every merchant carries a category code assigned by its payment processor. Costco, Sam's Club and BJ's sit under wholesale or warehouse clubs, a separate code from supermarkets.
Most cards that advertise a grocery bonus write it against US supermarkets and list warehouse clubs as an explicit exclusion. The food in the cart does not change the code.
A small number of cards do name wholesale clubs as a bonus category of their own. If you shop at a warehouse club often, that is the clause to look for, not the grocery one.
The card network decides first
In the US, Costco warehouses accept Visa credit cards and debit cards from most networks, but not Mastercard, American Express or Discover credit cards. A grocery card on another network cannot be used at the register at all.
That makes the realistic choice at the warehouse the best Visa card you carry. Often that is a flat-rate card or a card with a broad everyday category.
Costco's own Visa pays its highest rates on gas at Costco, then restaurants and travel, then Costco purchases, with a cap on the gas category. Compare it on those categories rather than on groceries.
Costco gas is a different question
Fuel at a Costco station is usually coded as a gas station, separately from the warehouse. Cards with a gas bonus generally earn it at the pump even when they earn the base rate inside.
The Visa-only rule applies at the pump too, so the best gas card you hold only helps if it runs on Visa.
If you fill up and shop on the same trip, two different cards can be the right answer within a few minutes of each other.
