Guinea pigs can be encouraged to use a litter box, but are rarely reliable. Sometimes they will use it and sometimes they won’t.
If you want to try to get a guinea pig to use a litter box, place one (a corner cage box or a small box for kittens work best) in its cage. Put the litter box in the spot in the cage where it goes to the bathroom most often. Put a handful of hay inside the box, along with a few fecal pellets. This will encourage your small pet to use the box as a bathroom.
When you see a guinea pig use the litter box, give it a treat as praise. Ignore mistakes it might make, but reward a guinea pig when it does correctly uses the litter box.
If a guinea pig starts using its litter box regularly while in its cage, you can try providing a box for it during out-of-cage time as well. A guinea pig is less likely to use it while loose in a larger space, but it may try.