Trump got hit by two gag orders from two different judges. The precedent in this area of law is severely underdeveloped, both because so few defendants get gagged, and of those who do very few have the resources or energy to mount an appeal. Jacob Sullum writes a great overview of the issue:
U.S. District Judge Tanya Chutkan, who is presiding over Donald Trump's trial on federal charges related to his attempted reversal of Joe Biden's 2020 election victory, yesterday imposed a gag order that bars the former president from "publicly targeting" witnesses, prosecutors, or court personnel. Trump lawyer John Lauro vigorously opposed the order on First Amendment grounds, saying it would stop his client from "speak[ing] truth to oppression." While that characterization exaggerates the order's impact, constraining the speech of a criminal defendant, especially one who is in the midst of a presidential campaign, does raise largely unsettled constitutional issues.
Chutkan's order was provoked by Trump's habit of vilifying anyone who crosses him, including Special Counsel Jack Smith ("deranged"), the prosecutors he oversees (a "team of thugs"), and Chutkan herself (a "highly partisan" and "biased, Trump Hating Judge"). "IF YOU GO AFTER ME, I'M COMING AFTER YOU!" Trump wrote on Truth Social after his indictment in this case. The next day, The New York Times notes, "a Texas woman left a voice mail message for Judge Chutkan, saying, 'If Trump doesn't get elected in 2024, we are coming to kill you, so tread lightly.'"
So to avoid the common pitfalls in discussions like this, set aside Trump for a moment. Should judges have any authority to impose gag orders? If so, what limits should be in place? After working out the questions in theory, how does your position apply to Trump?
I think gag orders can be appropriate in a very limited set of circumstances. For example let's say a newspaper owner is charged with murder in a small town. He should always have the absolute and unrestricted right to discuss the allegations against him and mount a defense in public. There are some areas where it starts to get cloudy for me, like for example if he hires an investigator (legal & appropriate) to dig into the history of the main witness against him (legal & appropriate) and he gets his hands on her diary (potentially legal & potentially appropriate). He then spends the lead-up to the murder trial publishing massive coverage the lurid details of all her sex kinks and fantasies, which isn't implicated in the murder charges. To me this starts to look like witness tampering/intimidation, where the defendant is humiliating a witness with the intent to discourage her from giving testimony.
So my answer is here would be yes, judges can impose gag orders but they should be extremely narrow. The operating principle should be to always allow defendants to discuss the direct charges against them, including the ability to discredit witness credibility. There's a blurry line between when someone is discrediting a witness on relevant matters, and when they're just trying to make their life hell to discourage them from testifying. An example of this blurry line is what happened to SBF, where his pretrial release was revoked in part because he leaked Caroline Ellison's diary to the NYT and because he seemed to have been coordinating testimony with FTX's general counsel.
So with that out of the way, how does it apply to Trump? Judge Chutkan's order restricts him from making statements that "target" the prosecutor, court staff, and "reasonably foreseeable witnesses or the substance of their testimony". Practically speaking, it goes without saying that it's a terrible idea to talk shit about the court or prosecutor while your case(s) is pending. There are some obvious areas where Trump's commentary is inane and irrelevant, like posting a photo of a judge's law clerk and claiming she's "Schumer's girlfriend", or posting about the prosecutor's family members. Discrediting witnesses is harder to draw a clean line on, because again there's a gradient between discrediting and intimidating. I think Trump should have the absolute and unrestricted right to discuss any of his charges and discredit any evidence and witnesses against him. While I disagree with that part of the ruling, I don't know how I would rephrase that clause, and so my reaction is that in these close cases we should default to allowing speech rather than restricting it.
Edit: guajalote changed my mind on the propriety of Judge Engoron's order prohibiting "personal attacks on my members of my court staff". I agree that criticizing government officials should always be protected, even if the speech is targeting irrelevant or uninvolved individuals. A narrower order prohibiting incitement would've been more appropriate.