An apple a day keeps the doctor away” is a proverb from the second half of the 19th century illustrating that apples make one healthy ("keeps the doctor away"). Many things can “keep the doctor away”—not paying medical bills, for example.
“An apple a day keeps the doctor away, but an onion a day keeps everybody away” has become the standard joke version of the proverb. “An onion a day keeps the doctor away...and everybody else” has been cited in print since at least 1903.
28 December 1903, Newark (OH) Advocate, “Fun of the College Boys,” pg. 6, col. 3:
A. D. Vyse—An onion a day keeps the doctor away.
X. Sepshun—Yes, and everybody else.
-- Pennsylvania Punch Bowl.
19 July 1908, Baltimore (MD) American, Some Reflections Of Uncle Ezra., features section, pg. 7, col. 1:
An old sayin’ is “that an apple a day keeps the doctor away.” By ginger! an onion a day will keep everybody away.
11 May 1910, Iowa City (IA) Daily Press, pg. 6, col. 4:
Apple vs. Onion.
She—They say that an apple a day will keep the doctor away.
He—Why stop there? An onion a day will keep everybody away.—Boston Transcript.
Google News Archive
23 December 1915, The Day (New London, CT), “On the spur of the moment” by Roy K. Moulton, pg. 6, col. 6:
An old sayin’ is, apple a day keeps the doctor away.” By ginger, an onion a day will keep everybody away.
Naughty 90’s Joke Book and Dyspeptic’s Guide to the Grave
By Harold Meyers
New York, NY: Avon Book Company
An apple a day keeps the doctor away; an onion a day keeps everybody away.
Fundamentals of Folk Literature
Edited by George W. Boswell and J. Russell Reaver
Oosterhout: Anthropological Publ.
(Also a Missouri man who liked his privacy used to eat an onion every day for breakfast with the remark, if questioned about his habit: “An apple a day keeps the doctor away; an onion a day keeps everybody away.")
21st Century American English Compendium:
A portable guide to the idiosyncrasies, subtleties, technical jargon, and conventional wisdom of American English
By Marvin Rubinstein
Rockville, MD: Schreiber Pub.
An apple a day keeps the doctor away.
However, an onion a day keeps everyone away.