The Pittsburgh Pirates’ offense hit another low Sunday afternoon, when a 28-year-old pitcher who was an undrafted free agent held them scoreless for 5 1/3 innings in his first major league start.

The Pirates looked hapless against San Diego right-hander Stephen Kolek in a 4-0 loss to the Padres, who completed a three-game series sweep before 17,184 at PNC Park.

It was the fourth consecutive loss and eighth in 10 games for the NL Central last-place Pirates (12-23), who were swept at home for the second time this season and shut out for the sixth time.

“Yeah, it’s frustrating,” Pirates manager Derek Shelton said. “Offensively, we need to be better. We haven’t been consistent. We haven’t been as good as we can be. There’s more in this offense. We’ve got to get to it. We’re trying different things. … Right now, we don’t have a formula, and we’ve got to figure it out.”

The Padres (22-11) did their damage against Pirates lefty Andrew Heaney, who allowed four runs on eight hits and season-high four walks with three strikeouts in 3 2/3 innings. It marked the second straight start in which Heaney (2-3) went five innings or fewer and allowed at least four runs.

“Just nibbling and then not making pitches when I needed to,” said Heaney, who threw 61 of 100 pitches for strikes.

Added Shelton: “It looked like he wasn’t missing by much. He was missing. They were balls, but he wasn’t missing by much, and they did not expand the zone.”

Manny Machado hit a two-out double and scored on a single by Xander Bogaerts to give the Padres a 1-0 lead in the first inning. Luis Campusano and Oscar Gonzalez drew back-to-back, full-count walks to load the bases, but Heaney got Jose Iglesias to ground into a forceout.

Elias Diaz hit a leadoff home run 380 feet to left in the second. Brandon Lockridge followed with a double down the right-field line, Luis Arraez added a single to short and both runners scored when Bogaerts doubled to left to give the Padres a 4-0 lead.

Heaney was pulled with two outs in the fourth after Machado singled and Bogaerts and Campusano walked to load the bases. Colin Holderman struck out Oscar Gonzalez to escape.

The Pirates couldn’t get their offense going against Kolek, who made 42 appearances as a reliever for the Padres last year but started this season at Triple-A Albuquerque, where he had a 6.38 ERA and 1.54 WHIP in five starts.

The Pirates had runners on first and second with one out in the second inning when Ke’Bryan Hayes was caught between the bases by Kolek on a pickoff play to end the inning.

Kolek kept the Pirates scoreless while going through the order twice in 5 1/3 innings, striking out four while holding them to four hits before exiting after walking Bryan Reynolds on his 84th pitch. Joey Bart doubled off righty reliever Jeremiah Estrada, who recovered to strike out Enmanuel Valdez and Hayes and strand both runners.

In the seventh, Matt Gorski pinch-hit for Adam Frazier and knocked one into the North Side Notch for a triple. But Gorski ran on contact on Jared Triolo’s sharp grounder to third and was thrown out at home by Machado.

Shelton said he talked about the play with Gorski, a 27-year-old rookie playing in his sixth major league game, but had no intention of making an example of him by pulling him out of the game.

“He made a young mistake. He’s never been on the field with Manny Machado and know that, in a four-run deficit, ball to his right-hand side, he’s got to learn,” Shelton said. “It wasn’t a lack of effort play. It was a lack of awareness play. We have to talk to him. If that ball is hit to Bogaerts or to Iglesias, he scores. But he’s got to know, a ball hit to Manny Machado’s right — maybe one of the best defenders in the last 50 years — he probably is going to get flat footed. Manny is the one guy that does have the arm to make that throw, but he’s going to throw the ball to the plate. It’s a learning moment for Gorski. It sucks in the moment because it ended up costing us a run, but embarrassing the kid right there, I’m not going to do that.”

Singles by Hayes and Gorski sparked the Pirates in the ninth. With two outs, Andrew McCutchen pinch-hit for Triolo but went down swinging to end the game. The Pirates went 0 for 7 with runners in scoring position and left eight on base, leaving Shelton searching for answers.

“I don’t know. That’s something I’m probably going to think a lot about tonight and have been thinking a lot about,” Shelton said. “But there has to be some sort of consistency. This lineup’s better than this.”