Peace be upon you and God's mercy and blessings.
Ayatollah Khamenei and Ayatollah Sistani (may Allah protect them):
In a sale where the price or the item being sold is from prohibited sources (like stolen or usurped money, or money resulting from selling prohibited items), there are two scenarios:
First Scenario: If the sale is specifically for that prohibited item, such as saying: this shirt in exchange for this dinar, and either the shirt or the dinar is prohibited, this is called personal sale. In this case, the sale is invalid, and neither party owns what was transferred in this sale. The person who received the lawful money must return it to its owner, just as in our example with the shirt. The prohibited money must be returned to its original owner if known; if the owner is unknown and cannot be identified, the ruling of unknown ownership applies, and it should be given as charity on behalf of its owner.
However, if you have the consent of the original owner regarding your use of their money, you are not required to return it or give charity on their behalf. This consent may be inferred if the original owner bought wine from someone, and that person used the proceeds to buy the shirt from you. If you know that the person who bought the wine is certainly agreeable to the second seller's use of their money, then if the second seller buys the shirt from you with that money, the sale is valid, and the money is not prohibited.
It seems that what was mentioned in the answer to your question refers to this scenario, as you stated: the money itself is prohibited. However, your expression may have misled you, and you might have meant the following scenario.
Second Scenario: If the sale occurs for the shirt without specifying which dinar will be paid in exchange, this is called a sale of a general obligation, which is common in transactions among people, as sellers usually do not intend to specify this dinar or that one. In this case, the sale is valid, and the seller can demand lawful payment from the buyer, as the prohibited money given to them does not belong to them to pass on to others. However, if this prohibited money remains in your possession, it must be returned to its original owner or given as charity on their behalf, as mentioned in the first scenario. The same ruling applies if you know the original owner's consent.
May you be successful in all good.
Source:
Ayatollah Sistani (may Allah protect him): Official website: 1, 2, 3.