Yoenis Cespedes lifted a fly ball in the ninth inning on Tuesday that sailed deep enough to offer a glimmer of hope. Off the bat, it appeared to be a walk-off homer. And even when it became clear that he didn’t get quite enough, it looked to be at least deep enough to advance Curtis Granderson to second.

But that turned out to be a letdown as well.

Centerfielder Tommy Pham threw out Granderson at second, and James Loney grounded against Cardinals closer Seung Hwan Oh to end the Mets’ 3-2 loss in the front end of a doubleheader at Citi Field.

“The guy’s got to make a great throw from the warning track,” said Mets manager Terry Collins, who took no issue with Granderson’s aggressiveness. “Sometimes you’ve got to tip your hats to the other club. I thought Grandy needed to get aggressive. We’re not scoring. We’ve got to start to create some runs.”

The Mets stranded 11 and finished 0-for-5 with runners in scoring position, a season-long problem. The Mets dropped to 52-46, half a game behind the 53-46 Cardinals, who were tied with the Marlins for the second wild card.

The Cardinals’ Matt Adams was thrown out at the plate trying to score a run and Aledmys Garcia made the first out at third base — breaks that went the Mets’ way. But the Mets blew their best chance by not being aggressive enough.

With two outs in the third, coach Tim Teufel held up the speedy Jose Reyes, who appeared to have a chance to score from third base on Cespedes’ double. Loney walked to load the bases for Kelly Johnson, who struck out.

Said Collins: “I’m not going to get into the coaching stuff.”

Noah Syndergaard (9-5) allowed three runs (two earned) in six innings. Despite laboring through much of the afternoon, Collins left him in for 115 pitches, equaling a season high.

It didn’t take long for Syndergaard to know that it might not be his day. The Cardinals took a 1-0 lead in the second inning on an unearned run. The rally materialized even though the ball left the infield just once.

“I just think the Cardinals did a really good job of getting my pitch count high in the first two innings,” said Syndergaard, who had eight strikeouts. “There were a series of unfortunate events that happened in the second inning that got my pitch count elevated. At that point I’m trying to last as long as I can out there knowing that it’s a doubleheader, trying to give the bullpen a break.”

Following Yadier Molina’s one-out single to center, Jeremy Hazelbaker legged out a slow roller to third, and Pham lined a comebacker off Syndergaard’s leg to load the bases. Up came pitcher Carlos Martinez, who bounced one to the right of the mound. Falling away, Syndergaard reversed his course, snagged the ball and attempted a throw home as he was falling down. The ball bounced in the dirt — Syndergaard was charged with a throwing error — and Molina scored.

In the third, Syndergaard paid dearly for a one-out walk to Matt Adams. Jedd Gyorko followed with a two-run blast down the leftfield line that hit the facing of the upper deck, giving the Cardinals a 3-0 lead.

Rene Rivera cut the deficit to 3-2 with his two-run homer in the fourth. Not known for his bat, the backup catcher has nonetheless delivered a handful of big hits for the Mets. It was his fourth homer.

The homer scored Asdrubal Cabrera, who reached on a hit by pitch, which came only after umpires reviewed the play and saw that the ball had struck the top of his foot.

The Mets fell to 5-5 after the All-Star break, continuing a pattern in which they have yet to win or lose consecutive games.