James Knapp, 45, of Sacramento, pleaded not guilty Tuesday to charges that he brought a .45-caliber handgun and a loaded magazine through the security checkpoint of a San Mateo County courthouse on July 12 — while en route to a child custody hearing, authorities said.

The weapon and ammo were found in Knapp’s backpack as it went through the x-ray machine around 1 p.m. at the Hall of Justice in Redwood City, the county sheriff said.

Knapp claimed he was unaware they were in the backpack, officials said.

Knapp also allegedly allowed authorities to search his car, where another magazine for the handgun was found in the glove compartment, according to San Mateo County prosecutors.

He was initially booked into the Maguire Correctional Facility and was later bailed out on a $50,000 bond, authorities said.

Knapp was headed to a family law court hearing in order to petition for a modification of his child custody order, authorities said.

The case is set for superior court review Oct. 13, with a preliminary hearing Oct. 26, Assistant District Attorney Karen Guidotti said.