“This did not work out as planned,” Ms. Sanders said later.

The plan, unbeknown to any but a few White House officials, had been in the works for some time, according to Ms. Sanders. Having flirted publicly with the prospect that the president would visit the demilitarized zone while he was in Seoul, 35 miles to the south, White House officials had made a show of shooting down the idea, first saying that Mr. Trump was unlikely to go, and later flatly ruling it out.

One senior aide went so far as to dismiss the notion as a “cliché,” noting that Vice President Mike Pence had recently made the trip to peer across the border with the North, as had Jim Mattis, the secretary of defense, and Rex W. Tillerson, the secretary of state.

It was difficult to believe that Mr. Trump, a showman who makes little secret of his love of all things militaristic, would forego a chance to visit the site, a staple of presidential trips to the region in the past, particularly during a trip dominated by talks about how to confront the threat from North Korea.

But Mr. Moon did not want him to make the trip, officials said, perhaps worrying about allowing the famously unscripted Mr. Trump, who has insulted Mr. Kim, the North Korean leader, as “Little Rocket Man,” to a place where any errant remark would take on graver significance.