It was supposed to be a quick photo op with President Trump.

But the 44 sheriffs at the White House got a lot more than that when Trump conscripted them as unwitting bystanders in a withering assault Wednesday on a critical, anonymous essay about him in the New York Times.

In a surreal setting, the president turned to the uniformed law enforcement officers, assembled on a small riser in the stately East Room, for explicit support as he attacked the “dishonest media” as a “disgrace.”

“Hey, I’ll ask the sheriffs: Can you imagine?” Trump said, responding to a reporter’s shouted question about the opinion piece. It was purportedly written by a senior official in the Trump administration who suggested a “quiet resistance” of fretful aides who were conspiring to protect the nation from an unstable president.

For the sheriffs, wearing gold badges and embroidered stars, it was a moment of truth: How would these elected officials — frozen in place in the background of a live television shot — react as Trump went on for another 840 words, slamming the anonymous author as “gutless,” predicting that the “phony media outlets” will go out of business and boasting of his accomplishments as if delivering a campaign speech?

To them, it was a no-brainer: They gave Trump several rounds of hearty applause.

“My president is a very strong-willed person. He speaks from the heart, and he put the gentleman in his place,” Sheriff Sam Page of Rockingham County, N.C., said in an interview Thursday. “You poke the bear, the bear pokes back. And if you’re in the media, you should understand.”

The remarkable scene was the latest escalation in Trump’s war against the media as he has sought to discredit stories about the special-counsel investigation into Russian election meddling and an unflattering new book from Pulitzer Prize-winning journalist Bob Woodward.

Trump routinely rails against the press corps on Twitter and at his campaign rallies. But he has begun eliciting cheers of support during more-official settings and from audiences once thought to be more immune to naked partisanship.

During a July speech at the Veterans of Foreign Wars’ national convention in Kansas City, Mo., Trump drew some of the loudest applause from the elderly veterans when he attacked CNN and NBC and told his audience: “Just stick with us. Don’t believe the crap you see from these people — the fake news.”

Some pundits and liberal activists criticized the sheriffs for appearing to take sides. But to the sheriffs, most of whom won office as Republicans, it was the press corps that was out of line.

“He made me very proud how he handled himself,” said Sheriff Mike Lewis of Wicomico County, Md. “I thought he was in control. As you could see, he turned and directed his comments many times to the sheriffs. We reassured him, ‘We’ve got your back, Mr. President, because you always have ours.’ ”

Most of the sheriffs were unaware of the Times op-ed, which had been published online shortly before the East Room ceremony. The group had come to the White House as part of a week of lobbying in Washington, including on Capitol Hill, for stronger immigration laws.

Before assembling in the East Room for the photo op — which was not on their itinerary and had not been listed on the president’s public schedule — the sheriffs met in the Eisenhower Executive Office Building with leaders from Immigration and Customs Enforcement and from Customs and Border Protection.

Vice President Pence dropped in briefly, shaking hands with each of the sheriffs and posing for at least one selfie.

Then the sheriffs were escorted across the street to the White House for the surprise event with Trump. Sheriff Thomas Hodgson of Bristol County, Mass., who had organized their visit, presented the president with a plaque bearing Trump’s name on a sheriff’s badge and the inscription, “There’s a new sheriff in town.”

Trump stood at a lectern and praised them for doing an “amazing job” despite being “scorned” by the “very, very dishonest” media. As the president wrapped up his remarks — lasting 360 words — reporters yelled out to him about the Times essay.

“Sheriffs are about fairness,” Hodgson said. “We work with the press all the time, but when we see something like that, which is wholly unfair, sheriffs bridle at that. We had just honored this man for what he’s done.”

When Trump responded forcefully, Hodgson added, “nobody looked at each other and said, ‘Let’s clap.’ But we all felt something at that moment.”

Sheriff Frederick Brown of Hunterdon County, N.J., who was in the front row, said he knew nothing about the newspaper opinion piece. But, he said, “the president knew we were supporting him, and that’s why he kept looking back at us. We did support him. It was almost perfect for him.”

Afterward, the sheriffs quickly discovered they had been part of the biggest news story of the day — and the attendant controversy.

Jonathan Darling, a former newspaper reporter who serves as public information officer for the Bristol County Sheriff’s Office, posted photos and videos to the department’s Facebook page.

Many followers posted positive comments — including a “MAGA” reference — but others were less impressed.

“Why were you all so eager to bow down to Tr*mp and allow him to attack our free press?” one person wrote. “I thought the police were supposed to work for all of us, not just the ones at the top?”

The sheriffs’ phones lit up with text messages from friends and family who had watched on television. Wicomico County’s Lewis said people texted him images of his face looking shocked. He posted one picture on Facebook and asked friends to write funny captions.

“We were all thrilled to have a front-row seat to what occurred in that room,” Lewis said. “We got to see our president chewing some serious ass.”