PITTSBURGH -- Jeff Locke said the formula for shutting down the St. Louis Cardinals was simple.

"Make them hit the ball on the ground," the Pittsburgh Pirates left-hander said. "It's that way with any team. The more they hit the ball on the ground, the less damage they can do."

Locke pitched into the eighth inning, getting 15 of his 22 outs on ground balls, and first baseman Ike Davis hit a two-run home run to lead the Pirates to a 3-1 victory over the Cardinals on Wednesday.

"He had the ball on the ground the whole time he was out there," Pirates manager Clint Hurdle said of Locke. "Any time they got a guy on, he was able to make pitches and stop any potential rallies that came his way."

Locke (6-3) allowed one run on six hits in 7 1/3 innings while walking two and striking out three. He also had two hits while improving to 4-0 in August with a 2.90 ERA in five starts.

"He was below the zone a lot and he kept going back and forth from one side (of the home plate) to the other throughout the game," Cardinals manager Mike Matheny said.

Davis' second-inning homer opened the scoring and came a day after his three-run shot as a pinch hitter in the eighth inning gave the Pirates a 5-2 victory over the Cardinals.

Davis was informed by Hurdle last weekend that he had lost his starting first baseman's job to Pedro Alvarez, who moved from third base. However, with Alvarez sitting out with a sprained left foot, Davis got his first start in a week.

"He's showing up to the ballpark ready to contribute any way he can," Hurdle said.

Third baseman Josh Harrison and catcher Russell Martin also had two hits each for Pirates, who won for the fifth time in their last seven games.

Closer Mark Melancon pitched a scoreless ninth inning for his 24th save in 28 opportunities.

The second-half struggles of Cardinals right-hander Adam Wainwright (15-9) continued as he lost his second consecutive start. Wainwright gave up three runs and eight hits in six innings with one walk and five strikeouts.

Wainwright fell to 3-5 with a 4.68 ERA in eight outings since starting for the National League in the All-Star Game. He was 12-4 with a 1.83 ERA in 19 first-half starts.

"I'm just going to go out there and keep battling until I'm back to where I want to be," Wainwright said. "I know I'm close. But I've been pitching like this for too long and I need to get back to being the pitcher I know I can be."

Cardinals second baseman Daniel Descalso had two hits. Left fielder Matt Holliday accounted for St. Louis' only run with a solo home run in the third inning as the Cardinals lost for the third time in the their last four games.

The Pirates drew within 4 1/2 games of Milwaukee in the NL Central while the Cardinals fell two games off the Brewers' pace. St. Louis' lead for the first NL wild card was sliced to 1 1/2 games over San Francisco while the Pirates got within one game behind the Giants for the second wild card.

Davis opened the scoring with his 10th home run of the season in the second inning and Holliday countered with 13th homer to slice in the top of the third to slice the deficit to 2-1.

Center fielder Andrew McCutchen restored the Pirates' two-run lead with a sacrifice fly in the bottom of the third. MuCutchen was back in the lineup after leaving Tuesday night's game with discomfort in his left ribs.

NOTES: Pirates RF Travis Snider left the game in the third inning with left hamstring discomfort after hitting a single and is to be reevaluated Thursday. ... Cardinals C Yadier Molina (torn ligaments in right thumb) is scheduled to start a rehab assignment Wednesday night with Double-A Springfield and catch five innings against Arkansas in a Texas League game. ... St. Louis C A.J. Pierzynski, 2B Kolten Wong and RF Oscar Taveras were all rested in a day game after a night game. ... Both teams are off Thursday then return to action Friday night. The Cardinals host the Cubs with St. Louis RHP Shelby Miller (8-9, 4.26) facing Chicago RHP Kyle Hendricks (5-1, 1.78). The Pirates host the Reds with Pittsburgh RHP Edinson Volquez (11-7, 3.56) squaring off with Cincinnati RHP Mike Leake (10-11, 3.51).