Focusing Search

Improving the built in search
functionality of WordPress

By Flynn O’Connor - @thoronas

My name is
Flynn O’Connor

  • Developed WordPress themes for 4 years.
  • Lead developer at Forge and Smith

Search is like any other WP_Query

This means we can use any of the parameters available to WP_Query for our search query.

  • Custom Post Types
  • Taxonomies
  • Meta values

Take Control

Inputs added to the form can control the query. But they must match the corresponding query parameters.

The results

what your form looks like

form1

What your query string looks like

query3

Say hello to my little friend

pre_get_posts

This action will alter the query before it is run.
With this you can do complicated search queries
involving taxonomies and meta values.

Search using custom meta data

Filter posts based on custom post meta.

If you have large chunks of content in custom meta use the "like" comparative parameter

Hide Content

Sometimes you don't want specific content to be searchable. You can specify that content in several ways.

  • Hide pages or posts
  • Hide categories
  • Hide entire post types

Changing the structure

If you allow search for multiple post types
you will probably have different
structure/layout for your content.

Your content should be consistent

If your content looks like this on your site.

listing

But your search results look like this

search-results

It is a bad user experience.

The elephant in the room

You can sort search results by:

  • Date
  • Name
  • Menu Order
  • Random

The major problem with the built in WordPress search: search results can’t be sorted by relevance
to the search term.

Relevanssi

relevanssi

Default search results ordered by relevance instead of date.

Non relevant
search results

results1

Relevant search results

results2

Control what gets searched

Without touching the code you can enable searches within:

  • Custom Post Types
  • Taxonomies
  • Custom Meta Fields

Allows keyword searches in all of the above

Plays nice with others

Because we are using pre_get_posts and passing valid query variables via form elements all of our custom search queries will work with Relevanssi right out of the box.

Moar powar!

If you want more control over your results
or search analytics.

  • Elastisearch
  • Swiftype

Elastic Search

elasticsearch.org

An open source search and analytics engine. Installs on your server. Provides analytics, discover popular search terms.

Pros

  • Scalability
  • Real time analytics
  • Open Source
  • Reduce load on SQL server
  • Fast

Cons

  • Difficult to set up for non-devs
  • Managing indices can be complex

Cloud based
Elasticsearch

Services like Searchbox.io provide a cloud based Easticsearch for WordPress sites. Easier for non-devs to set up. Removes some of the complex configuration required.

Provides an easy to use plugin WP-Elasticsearch for simple integration through WordPress dashboard.

Swiftype

swiftype.com

Cloud based search service, provides easy to implement search alternative.

Pros

  • Easy to set up.
  • Dashboard allows you to customize search results.
  • Detailed analytics

Cons

  • Limited free service, pay for all features
  • Custom search forms don't play well out of the box

Thanks for listening!

Questions/Comments?

Contact me:
Email: flynnoconnor@gmail.com
Twitter: @thoronas
fsquaredesign.com/wordpress-search/

WordCamp Vancouver 2013

August 17th

wordcamp-vancouver

Tickets on sale June 17th!

Speaker submissions until July 3rd.

http://2013.vancouver.wordcamp.org/

Social Media galore!

Follow us on: