Let’s face it; WordPress search sucks.
Its harsh, I know, but its true. Google didn’t dominate the Internet because they found decent search results. They’ve built an empire upon sophisticated algorithms that help us find extremely specific things amongst the trillions of possible pages.
WordPress publishes the content. Let’s let Google find it.
This tutorial will show you how to use a Google Custom Search Engine (CSE ) to replace the WordPress search engine and integrate it seamlessly into your website.
Note that Google CSE will only search pages they have indexed. Meaning, if you setup your site today then the Google bot may not have crawled it and made your pages available for search.
Setup your search engine
1. The first step to using a Google CSE is to use your Google account to create the search engine. Go to http://www.google.com/cse/ and once you are logged in, click “Create a Custom Search Engine”.

2. Next, name your search engine, provide a description and then add the domains you would like to search (your WordPress site’s domain and any others you’d like to include).
3. Click “Next”.
4. On the “Try it out” page you can just click “Next” again.
5. At this point you’re given code to paste into your site. Ignore this code and click on the “Change the look and feel” link.
6. Choose the “Two page” layout.
7. Click “Save & get code”.
Copy & paste code to your site
1. Where it asks you to provide the search results URL, use your sites domain: http://example.com

2. Where it asks you to provide the query parameter, use the letter s. s is the query that WordPress uses to search the database.
3. Copy the search form code it gives you and paste it into searchform.php (or create this file if you don’t have one) in your theme. Replace the contents of this file.
4. Copy the search results code it gives you and paste it into search.php (or create this file if you don’t have one) in your theme. Only replace the loop (everything between the if ( have_posts() ) : and endif;. Your loop may start and finish a little differently than that.
Seriously, that’s it
Now anywhere you had a search form you should be able to enter a term, hit Enter and be taken to your search page with relevant results. Use the WordPress Search widget to place a search box anywhere on your site.
Some extra goodies though …
Check the “Include Google Analytics Site Search tracking code” box. Then go edit your Google Analytics profile and specify the search paramenter (remember … s).
Because you’re using the WordPress search query you are actually doing a WordPress database search behind the scenes, but because we replaced the loop with the new code we don’t see any results. The DevPress search page actually uses Google’s search and WordPress’ search. In the sidebar of our search results page we break search results out into specific categories just to give a bit more information and context so results.
Thank you for this great tutorial…just a couple days late :) But this is something everyone should consider to implement for their blogs or sites.
I like to use the Relevanssi plugin to enhance search
I’ve tried many (EVERY) WordPress search plugins and nothing works right or well enough or easy enough for a client to understand. I would love it if WordPress search was finally mastered by a single plugin, but so far its lacking. If however, using Google isn’t an option, Relevanssi is one of the better plugins.
[...] Using Google search within a WordPress site [...]
Hi Patrick
Regarding the ‘s’ as a query parameter.
Do we really need it ? since this is google search engine taking result from indexed pages ( by google ) I don’t understand why we need the WP’s query ‘s’ to look from database.
I have a WP project comming up that consists of mostly static html pages that I plan to keep them as php files outside database and include the pages right in the template file.
I really think I could make use of this google custom search, but am afraid that it might not work if it needs to use database search query.
[...] http://devpress.com/ [...]
[...] http://devpress.com/ [...]
Thank you so much, I was using a wordpress plugin, but it would not open in a new window. This is perfect!
[...] http://devpress.com/ [...]
Finally a solution that worked, I’ve been trying many other tutorials like this and they all said to specify the output page, that didn’t work at all for me, this seems to be working although I can’t control the page name for the results.
Thanks.
I have been using Google’s CSE duly specifying a search result page which uses a page template with Google code in it. You can see it here: http://cyberbrahma.com/
This tutorial obviates the necessity to make a page by using the theme’s templates and “s” operator. This is a better idea
S.K
Patrick, thank you for the article. We have a multisite setup in subdirectories. I’m looking for a global search that will automatically include new blogs in the results. Do you know if this would work with the Google solution and would it require any special configuration or additional sitewide indexing via other plugins?
[...] w witrynie (przykładowy wynik poniżej). Poradnik przygotowałem na podstawie artykułu Using Google search within a WordPress site. Tweet Kategorie: Szablony. Tagi: Google, Google Analytics, wyszukiwarka. Autor: Piotr Sajnog. [...]
Thanks, I’ve added it to my site. Had a little hiccup with Autoptimize interfering with Google’s inline CSS and JS, so I restricted it to minifying/caching scripts in the header only.
Nice website.
Would this index the content of any linked-to documents e.g. .doc, .txt, .pdf, .rtf, etc.? And therefore search results would include matches for text inside files as well as the usual post content, etc.?
Thanks in advance.
Rich
Thanks so much for your tutorial!!! I created a Twenty Eleven child theme and struggled with how to replace the search function with Google CSE. Who would have guessed it would be so easy? In my case, I had already created a CSE. I used the Results Only layout and only modified the search.php results file as instructed.
Oh my god. Thank you for this. I was screaming in frustration trying to figure out how to get this to work with the plethora of OLD guides referencing iframes and other nonsense that Google doesn’t support anymore.
O Ooh thanks alot I’ve been wondering, how I can ever do this, and all of a sudden a saviour came . thanks very much.
So I tried this, and the search form works just fine, but the results page does not.. I put the code into the search.php as the tutorial states, and ended up with blank search results.. I even tried creating a search results page and copying the search results code into a search results page I created. no blank page, but no search results either. I even tried putting the search results code into a page template and that also ended up with blank search results..
What am I missing here???
Hi, the nuts and bolts of the code as you have presented it works fine. However, it finds hardly anything on my site – even stuff that I know is indexed because I can locate it with the search term “stuff” site:http://www.mysite.com. Any ideas?
Ha ha! Please scrub my original comment! All I needed to do was to add /* to my URL in the list of sites to search and they all appear! So you type your URL as http://www.mysite.com/* and Bob is very much your mother’s brother!
[...] a detailed guide on how to set up Google Custom Search for your WordPress site, click here. There was once a functional plugin in the WordPress repository that carried out the background [...]
what if I dont want to put it on my search.php because i need both google custom search and wordpress search?
you’d just use google search codes normally instead of following this tutorial. this tutorial is only for splitting the search form and the search results into different areas.