Exclude category for main query and custom

It’s a custom query. In that case, you can pass the parameter directly to the custom query. Otherwise, you can add other conditions in ‘exclude_category’ function. for instance, You can check whether category, post type, archive or not.