LOS ANGELES — A federal judge on Wednesday blocked enforcement of a Los Angeles law requiring businesses that want city contracts to disclose whether they have ties to the National Rifle Association.

The NRA’s request for a preliminary injunction was granted by U.S. District Judge Stephen V. Wilson in Los Angeles. It temporarily prohibits enforcement of the measure while the case unfolds. The next step could be an appeal by the city or an NRA request to make the injunction permanent.

The judge also threw out part of the lawsuit on technical grounds and removed the city clerk and Mayor Eric Garcetti as defendants but he refused to entirely dismiss the lawsuit.

The ordinance that took effect in April was passed in response to mass shootings around the country, including a November 2018 attack that killed 12 people at a bar in Thousand Oaks, northwest of L.A. It requires those wanting city contracts to disclose whether they have contracts or sponsorship from the gun-rights group.

“Public funds provided to such contractors undermines the city’s efforts to legislate and promote gun safety,” the law said.

The NRA argued that the measure violates the constitutional First Amendment right to free speech and association and the 14th Amendment right to equal protection. NRA attorney Chuck Michel called it “modern-day McCarthyism” that would force NRA supporters to drop their memberships for fear of losing their livelihoods.

In his ruling, the judge said the city contends that granting contracts to those “with business ties to the NRA invariably creates more NRA membership, which leads to more pro-gun advocacy, laxer gun laws, and inevitably more mass shootings.”

“Even if this chain of logic was supported by fact, the city is not permitted to restrict political speech as a means of achieving its goal of safer cities,” Wilson ruled.

Messages seeking comment from an NRA spokesperson and attorneys for the city were not immediately returned.

The NRA has been battling a number of challenges to its operations in recent months, including an investigation by the attorney general in New York, where its charter was formed, and the attorney general in Washington, D.C., where authorities are questioning whether its operations are in violation of its nonprofit status. Several corporations also have cut ties to the group.

The NRA sued the city of San Francisco earlier this year over a September resolution by the San Francisco Board of Supervisors that declared the group a “domestic terrorist organization.” The NRA alleged the resolution, which had no legal weight, violated the group’s free speech rights. However, it withdrew the suit in November.