Trenitalia does not show the local train company in Puglia, Ferrovia Apulo Lucane. They provide service from Bari to Matera. But is is not direct; you have to change at Altamura and the overall journey time of 1h 48 minutes is longer than the time for a bus trip. And from what I have read, the train service is slow and often delayed. The website is a bit quirky but here is is:
https://ferrovieappulolucane.it/en/ricerca-corse/
There are several bus companies, and the easiest way to find them and book tickets is to use a website like Trainline.
https://www.thetrainline.com/en-us/train-times/bari-to-matera
Trenitalia will show you train ,or train plus bus, options to Martina Franca from Bari. But they all take around 2.5 hours. It is only 47 miles and the drive time is just over an hour. I know this because our fantastic guide Emanuela organized a private driver for us and 2 other couples on our guided Road Scholar trip when she heard we were planning to take the train.
Which brings me to my next comment. My husband and I generally only take tours for guided hiking in places like Patagonia or the Dolomites; we have never done a standard group tour (such as a Rick Steves tour) and in most cases would not be interested. But we wanted to visit Puglia, and we have decided after successfully renting cars in Italy without a ticket (but lots of stress) we would not do it ever again. I started planning a trip to the “usual suspects” in Puglia using public transport, and pretty quickly realized that we could not get to some of the places we wanted to see without a lot of wasted time and frustration.
So I looked around and found a Road Scholar tour of Puglia that was a perfect fit. This was Feb.-March of 2023. We flew into Rome, overnighted there and then traveled to Lecce by fast Frecce train. We planned 3 extra days on the front before meeting the tour there, and made good use of those, one with a daytrip to charming Monopoli (where my husband swam in the Adriatic on Feb. 24).
We were very pleasantly surprised at how good the tour was—-the lodging, the food, the locations and sites we visited, and especially the expertise of our guide Emanuela, who provided lots of information about the culture and history of Puglia and Matera. She organized experiences that we would not have found on our own, including a visit to the ancient olive grove and oil mill made famous by Stanley Tucci in one of his travel episodes. (I have a photo with the owner’s arm around me). When my husband and I got “lost” in the sassi of Matera on our free time and could not find our way back to the bus, I called Emanuela, told her where we were, and she gave us simple directions to a piazza halfway between, where she met us. And didn’t even scold us for our misdeed.
While we originally had misgivings about the size of the group—-22 people instead of the usual 8-10 we have on our guided hiking tours—-it turned out just fine. We enjoyed meeting such a variety of people, most of whom were younger than we are. The bus was very comfortable, and some part of each bus ride was given to mini-lectures on the Whisper sets.
I could go on, but will stop here and just point to the website for the trip. They have one in May 2025 that fits perfectly into your schedule—-May 5-21. You would actually meet them iin Lecce on May 6, so could have a few days there on your own first, like we did. The tour ends on May 21 after breakfast, so you depart back to Linate that day from Lamezia Terme international airport, in good time for your flight booked on the 22nd.
Note that this tour includes Calabria in addition to Puglia and Basilicata.
https://www.roadscholar.org/find-an-adventure/20221/Puglia-Basilicata-Calabria-Southern-Italy-From-Heel-to-Toe-