Getting Random Post Suggestions from Custom Google Search Engines

Update 3/31 - After more tinkering, I've changed the code to be more stable with any changes Google makes in the future. Code snippets are up to date and irrelevant instructions are now struck out.


You can create a custom Google search engine for your website (or group of websites) which can be very handy for a variety of reasons. If you've got a gigantic website, like Jerry Blumengarten, having a custom search bar can help find materials. If you're working with students, a custom search can help limit where their information comes from. They're easy to do, but there are some limitations, so be sure to read the Terms of Service if you're going to set one up.


John Stevens asked on Twitter if there was a way to get a random post from a custom search. I felt like there ought to be some way to do that, so I started digging. Because the MTBoS index of blogs is on all different platforms and not networked in any particular way, it turned into more of a chore than I originally thought. Really, when you get down to it, randomizing search results is the anthesis of what search engines do, so we have to accomplish the "random" aspect of the Google search with a different method.

I'll be using a search for my own blog for the demo here (down below). Brace yourself for some code.

First, rather than using the Google script you get from the Control Panel, use this HTML instead:

You're given one line of HTML to put in your page where you want to search bar to appear. Simply add the button line to add the random button:

google-cse-html

<!-- Creates the search bar -->
<gcse:search></gcse:search>

<!-- This fires the random search topic function -->
<button>Random</button>

Line 5 adds a new button - the Random button - to the search form. That button is important. Rather than needing some text in the input field, a user can click on the Random button and some JavaScript magic will happen.


If you want to use this, you'll need to do a couple things:

Make sure your site has jQuery installed. If you're not sure, go to your site's code and paste this in before the closing header tag:

You'll need to put a script in the footer of your site. You can also do this with a widget with a lot of platforms...there are too many to list, and the footer solution is usually the best approach. If you're not sure what to do, leave a comment, and I can try to help you out.

// This is the array holding the serach terms var queries = ['animals','dogs','cats', 'emu','fish','snakes', 'panda bears','meerkats','spiders','birds'];

// When you click on the Random button, it'll grab a random search term and run it through Google document.querySelector('button').addEventListener('click', function() { var search = google.search.cse.element.getElement('standard0'); var rand = Math.floor(Math.random() * queries.length); search.execute( queries[rand] ); }, false);

This script will take that Random button we added earlier and pop a random term into the search box and carry out the search. yOu need to set the terms in the array (line 3), each term in single quotes and each term separated by a comma. Make sure to keep that syntax correct or else it won't work.

Also, if you try this on any site other than your own, you can run into issues. I've been testing this on Codepen.io with mixed results. Once thrown into my own page, though, it works just fine. I'd create a new page on your site and drop the code into place. Test it as a draft page to make sure everything is kosher and then run with it.

If you want to see a sample implementation (proof of concept, not really helpful), check out this spartan search page. You can type in a search term and hit submit to see how it works. Then, try hitting Random without putting any text in to see what it does.

Do note that WordPress has a pretty good random post feature built-in. This is meant more for wider searches across multiple platforms using the Google index of sites.

One thought on “Getting Random Post Suggestions from Custom Google Search Engines

  1. Oh I love seeing people tinker. but I thought the request was to return a random result? I see what you did as a random search term suggestion?

    You could try something that returns a random start number (essentially a paged results start), but that will not work when the results are small, but just for fun, play with the last parameter on https://www.google.com/?gws_rd=ssl#q=dog&start=120

    There are some hooks into the search API via Javascript
    https://developers.google.com/custom-search/docs/element

    this is kind of funky too
    http://www.bananaslug.com/

Leave a Reply

Your email address will not be published. Required fields are marked *