Fast WordPress Searches

By: Ryan Kienstra on: January 8, 2015  in: WordPress VIP

Fast WordPress SearchesIf you have a lot of content on WordPress, you’ll need a fast way to search it.

Maybe you have a directory with an advanced search.

WordPress VIP agency 10up recently released their Engineering Best Practices.

These have many techniques for fast WordPress searches.

Prefer WP_Query To get_posts

10up explains that the get_posts function actually calls WP_Query.

But it doesn’t use the same filters.

And by using WP_Query, you can…

Reduce Queries

Normally, WP_Query does 5 queries.

But you can remove the ones you don’t need.

  • If you don’t need pagination, use 'no_found_rows' => true.
  • If you don’t need taxonomy terms, use 'update_post_term_cache' => false.

In my plugin, there’s a query for a drop-down menu of the most recent posts.

Pull-Down Of Posts

From my Adapter Post Preview plugin

There’s no need for pagination, like you would use for blog posts or comments.

So I added 'no_found_rows' => true to the WP_Query arguments array.

$query = new WP_Query( array( 'post_type'              => 'post' ,
                              'orderby'                => 'date' ,
                              'posts_per_page'         => '100' ,
                              'no_found_rows'          => true ,
                              'update_post_term_cache' => false ,
) );

(See the entire file on GitHub)

Limit Query Dimensions

10up recommends only passing one query dimension to the WP_Query object.

You can then narrow the results with PHP.

For example, I built a mockup of a directory for app developers.

It’s based on Seth Godin’s idea.

App Developer Directory

A directory of app developers

There are three query dimensions:

  • Keyword(s)
  • Price
  • Type of project

But I’ll only pass one to WP_Query.

The “Keyword(s)” search will return the fewest results of the three dimensions.

So I’ll pass that as a value of 's'.

$query = new WP_Query( array( 's' => esc_html( $keyword_input_value ) , 
                              'posts_per_page' => '200' ,
                              'post_type' => 'gdd_developer' ,
) );


Narrowing The Results With PHP

For each post in the loop, I’ll call the function has_term twice.

This evaluates whether the post has the right project type and price.

The project type that the user selected will be assigned to $gdd_project_type_value.

And the price will be assigned to $gdd_price_value.

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        if ( ( has_term( 'gdd_project_type' , esc_html( $gdd_project_type_value ) ) && has_term( 'gdd_price' , esc_html( $gdd_price_value ) ) ) {
            // display directory information 

The directory entry will only show if it has right project type and price.

When using three query dimensions, you’ll need fast WordPress searches.

Most search results won’t be cached because very few will be the same.

And some caching plugins like Batcache only store a page for 5 minutes.

Fewer WP_Query Operations

Key To Fast WordPress Searches

All of the techniques above reduce the load that WP_Query places on the database.

  • Only use 1 WP_Query dimension.
  • Try to remove some of the 5 default queries.

What’s your strategy for fast WordPress searches?

Do you have a limit of dimensions for WP_Query?

Leave a comment