NEW YORK >> Donald Trump had a dismal day in court Tuesday as the judge presiding over his criminal trial told a defense lawyer he was “losing all credibility” and a key witness pulled back the curtain to expose what prosecutors called a conspiracy to influence the 2016 election.

The witness was David Pecker, longtime publisher of The National Enquirer, and he transported jurors back to a crucial 2015 meeting with Trump and his fixer at Trump Tower in midtown Manhattan.

Prosecutors called it the “Trump Tower conspiracy,” arguing that Pecker, Trump and Michael Cohen, who was then Trump’s personal lawyer and fixer, hatched a plot at the meeting to conceal sex scandals looming over Trump’s campaign.

Their effort led Pecker’s tabloids to buy and bury two damaging stories about Trump. Cohen also purchased the silence of a porn actor, a deal at the heart of the case against the former president.

In gripping and pivotal testimony Tuesday, Pecker recalled how Cohen and Trump asked what he and his magazines — fixtures of American supermarket checkout lanes — could do “to help the campaign.” The account bolstered the prosecution’s argument that the men were protecting not just Trump’s personal reputation, but his political fortunes.

“I would be your eyes and ears,” Pecker recalled telling them, as he explained the tabloid practice of “catch and kill,” in which an outlet bought the rights to a story, only to never publish it.

Pecker’s testimony came after a bruising hearing for Trump and his legal team, as prosecutors argued that the trial is threatened by Trump’s repeated attacks on witnesses and jurors, mostly launched on social media and his campaign website. They urged the judge, Juan M. Merchan, to hold Trump in contempt over what they said were 11 violations of a gag order that bars the former president from attacking witnesses, prosecutors, jurors and court staff, as well as their relatives.

The case against Trump, the first American president to face a criminal trial, centers on Cohen’s $130,000 hush-money payment to porn actor Stormy Daniels. Prosecutors say he paid Daniels at Trump’s direction during the 2016 campaign to keep her quiet about a sexual tryst she said she had with Trump.

Trump, 77, who faces up to four years behind bars, is charged with 34 counts of falsifying business records for the way in which he accounted for the $130,000 repayment to Cohen. Each count reflects a different false check, ledger and invoice that, according to prosecutors, Trump used to disguise the reimbursement’s true purpose.

The gag-order hearing, held with the jury out of the courtroom, demonstrated a jarring reality for Trump as he seeks to reclaim the White House while under indictment: His political reflexes, and the norm-busting ethos that has defined the Trump era, often clash with the letter of the law.

In about 2 1/2 hours of examining Pecker on Tuesday, the prosecution placed him firmly in Trump’s orbit, as a longtime fan and friend who became an extension of the 2016 Trump campaign. His closeness to Trump — and his gentle, almost grandfatherly affect — appeared to bolster his credibility.

Pecker described a symbiotic relationship between Trump and The National Enquirer during the former president’s turn as a reality television host on “The Apprentice.”

Trump’s lawyers have not yet cross-examined Pecker, but when they do, they are likely to seize on that description of Cohen. A central theme of Trump’s defense is to portray Cohen as a renegade and a liar, and to distance the former president from the most problematic evidence.