Using Google search within a WordPress site

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”.

Screenshot of Google Custom Search Engine setup step 1

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

Screenshot of completed Google Custom Search Engine displaying code

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.

24 Comments

  1. Nicolas Messer
    Nicolas Messer September 1, 2011 at 7:40 am . Reply

    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.

  2. Paul
    Paul September 1, 2011 at 3:46 pm . Reply

    I like to use the Relevanssi plugin to enhance search

  3. Google searchh | Fivedollarsbyt
    Google searchh | Fivedollarsbyt September 2, 2011 at 5:23 am .

    [...] Using Google search within a WordPress site [...]

  4. Paul
    Paul September 2, 2011 at 11:20 pm . Reply

    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.

  5. Tyler
    Tyler September 6, 2011 at 6:31 pm . Reply

    Thank you so much, I was using a wordpress plugin, but it would not open in a new window. This is perfect!

  6. Ben Dixon
    Ben Dixon September 19, 2011 at 1:30 pm . Reply

    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.

    1. S.K
      S.K December 6, 2011 at 8:38 pm . Reply

      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

  7. Kathy
    Kathy October 2, 2011 at 7:24 am . Reply

    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?

  8. [...] 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. [...]

  9. Shaun
    Shaun October 16, 2011 at 6:49 am . Reply

    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.

  10. Richard Telford
    Richard Telford November 29, 2011 at 12:00 pm . Reply

    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

  11. Chad
    Chad December 13, 2011 at 6:21 pm . Reply

    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.

  12. Will Lanni
    Will Lanni December 14, 2011 at 5:56 am . Reply

    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.

  13. henry
    henry December 21, 2011 at 2:40 pm . Reply

    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.

  14. Crystal Jones
    Crystal Jones December 23, 2011 at 7:48 pm . Reply

    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???

  15. Simon Lewis
    Simon Lewis January 14, 2012 at 3:42 pm . Reply

    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?

  16. Simon Lewis
    Simon Lewis January 14, 2012 at 3:51 pm . Reply

    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!

  17. [...] 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 [...]

  18. wilbert
    wilbert January 24, 2012 at 11:07 pm . Reply

    what if I dont want to put it on my search.php because i need both google custom search and wordpress search?

    1. Tung Do
      Tung Do January 27, 2012 at 9:16 am . Reply

      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.

Post Comment