FAIRFAX, Va. — A three-term northern Virginia mayor was arrested on drug charges after he tried to trade two grams of methamphetamine for sex acts in what turned out to be an undercover sting operation, police said Friday.

City of Fairfax Mayor R. Scott Silverthorne was arrested Thursday night after meeting at a Tysons Corner hotel just outside the Capital Beltway with undercover detectives he approached through a website used to arrange casual sexual encounters between men, Fairfax County police said at a press conference.

Police Capt. Jack Hardin said they received a tip that Silverthorne had been arranging drugs-for-sex encounters. Police then set up an online profile on the website, and within two days Silverthorne made contact online with the detective who set up the profile, Hardin said.

“We had information on what the mayor was looking for, what types of activities” he wanted to engage in, Hardin said.

Silverthorne, who also worked as a substitute teacher in Fairfax County Public Schools, was arrested and gave a full confession, police said. He was released on his own recognizance while he awaits a preliminary hearing Oct. 31 on a felony charge of drug distribution and a misdemeanor charge of possessing drug paraphernalia.

Silverthorne, in an email to the Associated Press, declined comment and referred questions to his attorney, Brian Drummond, who did not immediately return a phone call Friday morning.

The city announced Friday that Silverthorne appointed Councilman Jeffrey Greenfield as acting mayor, effective immediately. The city charter gives the mayor the power to appoint an acting mayor in his place in the event of a mayor’s absence.

Greenfield issued a statement saying he would serve as mayor “until further notice.

“The City of Fairfax City Council appreciates Mr. Silverthorne’s longstanding dedication to the community. He has served the City of Fairfax as a Councilmember and Mayor for more than a quarter of a century. The community has benefited from his dedication and his vision,” Greenfield said.

Hardin didn’t know how long Silverthorne had been using the website. But he said drugs-for-sex encounters were common on the website, and that Silverthorne has set up a similar encounter at least once before.