As the Mexican–American War was beginning, O'Hara signed up for the U.S. Army on June 26, 1846. He held the positions of captain and quartermaster of volunteers under General Gideon J. Pillow as they advanced upon Mexico City, Mexico. For excellent conduct in the Battle of Contreras and the Battle of Churubusco, O'Hara was honored with the rank of brevet-major on August 20, 1847. He was honorably discharged on October 15, 1848. After the war ended in 1848, O'Hara returned to Washington, D.C. to continue his law practices until 1851.
Between Wars
O'Hara was a firm believer in American expansion, in the form of filibustering. He joined others from Kentucky in an expedition to Cuba in 1850, after spending much of 1849 recruiting Kentuckians to the filibuster cause. Under the command of General Narciso López, O'Hara commanded a regiment, with the rank of colonel, in the hopes of removing Spanish rule from Cuba. In the battle of Cárdenas on May 18, 1850, he suffered a severe injury. After Lopez failed and died in his Cuba position in 1851, O'Hara returned to Kentucky, after fellow Kentuckians serving in Cuba took him with them as they escaped, returning to the United States at Key West, Florida. O'Hara returned to journalism, first working for the Frankfort Yeoman of Frankfort, Kentucky, and then helping to found the original Louisville Times of Louisville, Kentucky in 1852, which became an organ for spreading Democratic Party propaganda. O'Hara left the Louisville Times in 1853 to join General John A. Quitman's filibuster expedition to Cuba. After Quitman's efforts failed, O'Hara attained a commission in the United States Army, was appointed captain of the Second Cavalry on March 3, 1855, and returned to Louisville as a recruiter for the Army. He was reassigned to Indian fighting on the prairies of Texas. On December 1, 1856, he was forced to resign by Robert E. Lee, as Lee brought up charges of drunkenness against O'Hara, and threatened him with court martial. When John Forsyth Jr., editor-in-chief of the Mobile Register of Mobile, Alabama became minister to Mexico in 1856, O'Hara took his place in the newspaper. He continued to follow government orders, such as his diplomatic mission into the Tehuantepec grant debate.
After the war, O'Hara went to Columbus, Georgia to work in the cotton business, but eventually he lost his business to a fire. He later lived on a plantation near Guerryton, Bullock County, Alabama, where he died. He was returned to Columbus for burial. On September 15, 1874, his remains, along with those of other Mexican War officers, were buried in the state cemetery at Frankfort Cemetery in Frankfort, Kentucky. His friend Sergeant Henry T. Stanton read "Bivouac of the Dead" at the reinternment and said, "O'Hara, in giving utterance to this song, became at once the builder of his own monument and the author of his own epitaph." Lines from the poem would eventually grace the gates of numerous national cemeteries and several monuments of Confederate Dead. In particular, the first verse's second quatrain is often quoted: On Fame's eternal camping-ground Their silent tents are spread, And Glory guards, with solemn round, The bivouac of the dead.
Because he served in the Confederacy, O'Hara often goes uncredited when the quatrain is used in non-Confederate settings. There is a dispute over when O'Hara wrote "Bivouac of the Dead". It is popularly thought to be written after the Battle of Buena Vista of 1847, where many Kentucky volunteers died. Others say it was actually written after the Battle of Cárdenas in 1851. The New York Times wrote that it was first published in the Frankfort Yeoman in 1850, which puts it before O'Hara's Cuba adventures.