WASHINGTON — The Republican-led House rejected a far-ranging immigration bill on Wednesday despite its eleventh-hour endorsement by President Donald Trump, as the gulf between the GOP’s moderate and conservative wings proved too deep for leaders to avert an election-year display of division on the issue.

The vote was 301-121, with nearly half of Republicans opposing the measure. The depth of GOP opposition was an embarrassing showing for Trump and a rebuff of House GOP leaders, who’d postponed the vote twice and proposed changes in hopes of driving up the vote for a measure that seemed doomed from the start.

The tally also seemed to empower GOP conservatives on the fraught issue. Last week a more conservative package was defeated but 193 Republicans voted for it.

Even if it passed, it would have been dead on arrival in the closely divided Senate, where Democrats would have had enough votes to kill it.

GOP leaders have been talking about a Plan B: a bill focused narrowly on barring the government from wresting children from migrant families caught entering the country without authorization.

With television and social media awash with images and wails of young children torn from parents, many Republicans want to pass a narrower measure addressing those separations before Congress leaves at week’s end for its July 4 break.

But GOP aides said Republicans had yet to agree on bill language. And the effort was complicated by a federal judge who ordered that divided families be reunited with 30 days.

Democrats voted en masse against the House legislation, which would provide a shot at citizenship for hundreds of thousands of immigrants brought illegally to the U.S. as children.

It would provide $25 billion for Trump to build his coveted wall on the border with Mexico, restrict family-based immigration and bar the Homeland Security Department from taking migrant children from parents seized crossing into the country without authorization.

In a startling turnabout earlier Wednesday, Trump made a last-minute pitch for the bill. Last Friday, he urged Republicans to stop wasting time on the effort until after the November elections.

In his latest instance of whiplash on the issue, Trump tweeted, “HOUSE REPUBLICANS SHOULD PASS THE STRONG BUT FAIR IMMIGRATION BILL, KNOWN AS GOODLATTE II, IN THEIR AFTERNOON VOTE TODAY, EVEN THOUGH THE DEMS WON’T LET IT PASS IN THE SENATE.”

The tweet — which referenced Rep. Bob Goodlatte, R-Va., one of the bill’s sponsors — could well garner additional votes for a measure that was still expected to fail Wednesday. It was also the latest example of Trump’s erratic dealings with Congress. On Friday he dashed off a tweet, saying Republicans should “stop wasting their time on Immigration until after we elect more Senators and Congressmen/women in November.”

Trump had heard from House Majority Leader Kevin McCarthy, R-Calif., who urged him to publicly support the bill, said a person familiar with the conversation who wasn’t authorized to speak publicly. McCarthy, who is hoping to succeed Speaker Paul Ryan as speaker next year, and Trump have forged a close relationship.

Hours later, the White House sent a letter to lawmakers restating its support, saying the legislation would “support the administration’s goals” on immigration.

___

AP reporter Catherine Lucey contributed.