I heard that a Jewish tradition says that praying for 40 consecutive days at the Western Wall ensures the prayer will be answered.
Does anyone where the source for this is?
According to Rav Yosef Shalom Elyashiv (quoted here), there is no basis at all in Judaism for this practice ("אין בזה שום עניין"). He says every prayer at the Kotel is accepted.
Note: 40 days of prayer at the wall is considered a "Segulah" and therefore relies more on tradition than the strict rationalist approach that the halachic process has.
An Article written by Rabbi Levi Friend, author of the book "Segulos HaBaal Shem Tov" for the Torah Journal "Ohr Yisroel" (Monsey) investigated this and other 40 day "Segulos" and came up with the following.
The excepted source for praying 40 days at the Western wall is from Rav Shlomo of Zhvil who lived the last part of his life in Jerusalem. Since he died in 1945 it is a fairly recent tradition. The tradition is mentioned in a book about his life "Tzadik Yesod Olam" Vol 2 page 39
The above mentioned article sites that the power and tradition of praying for 40 days is based on a passage in the Zohar (chadash) "Anyone who prays for 40 days will certainly be answered"
Based on this the author concludes that the power of 40 days of prayer on its own is powerful enough and you do not need to go specifically to the Western Wall for it to work. [The author suggests any place which is considered holy such as a Shul or Bes Medrish can add additional merits to the prayer.]
This is a combination of the belief that prayer at the Western Wall is particularly effective and the belief that praying for 40 days is particularly effective.
Sources For Prayer at Western Wall
The Western Wall itself is not emphasized in older literature. In The Lord is Righteous in All His Ways, pp. 206-207, the Rav discusses the Western Wall and says that there is no mention of it in Chazal and very little mention in rishonim (source). So older sources only reference the uniqueness of prayer in vicinity of the Temple itself (e.g. Pirkei D'Rabbi Elziezer ch. 35). However later sources (1) extend this to the Western Wall, connecting this to the statement in Shemot Rabba (2:2) that the Shekhina does not leave the Western Wall.
Prayer for 40 Days The idea that praying for 40 days is particularly powerful is possibly related to Moses praying for forty days (Deut. 9:18). Later Jewish literature also references 40 days of prayer (2). E.g. Devarim Rabba (Vatchanan 2):
מהו בכל קראנו אליו אמרו רבנן יש תפלה שנענית לארבעים יום ממי את למד ממשה דכתיב ואתנפל לפני ה' מ' יום וגו'
The idea of special efficacy of 40 days of prayer is also found in the Jami` at-Tirmidhi (9th cent. Hadith compilation).
“Whoever prays to Allah for forty days in congregation, catching up with the first takbeer, he will be recorded as being free from two things: free from the Fire and free from hypocrisy.” (source).
A similar activity was practiced by Sufis:
One of these ... was the practice of a spiritual retreat (halwa) for a period of forty days ('arba'un) still followed by Sufis in the Naqsabandi order in present-day Egypt. (3)
This was adopted by some Jews such as R. Hananel bar Shemuel (the father-in-law of Rambam's son R. Avraham)
By chance, a letter referring to “the noble dayyan” (i.e. R. Hanael ben Shemuel) in connection with the practice of this spiritual exercise was preserved in the genizah.(3)
Significantly, these sources precede the publication of the Zohar (Hadash) by several centuries, and are thus serious contenders for the earliest source for 40 days of prayers; one of the main components of the practice in question, and its significance.
(1) Tuv Yerushalayim p. 109 cited in Encyclopedia Talmudit: Kotel Ma`aravi.
(2) See also Otsar HaMidrashim (Eisenstein) Shimon ben Yohai p. 555.
(3) Paul Fenton’s A Judeo-Arabic Commentary on the Haftarot by Hanan’el ben Semu’el (?), Abraham Maimonides’ Father-in-Law, p. 31 in Maimonidean Studies (ed. Arthur Hyman) vol. 1. New York, 1990.