Offer ends in:
  
Black Friday: 40% off your first year: 40% off on annual plans. Use code with BF2026 at checkout. View Plugins

Better Search enhances WordPress’s native search capabilities to deliver more relevant results to visitors. At the heart of this functionality is the Better_Search_Query class, which you can use for optimal search performance on your site.

What is Better_Search_Query?

The Better_Search_Query class extends WordPress’s native WP_Query class, significantly improving search relevance and performance. It implements MySQL’s FULLTEXT indexing and provides advanced search operators that help users find relevant search results.

Implementing Better_Search_Query in Your Theme

Basic Implementation

To use Better_Search_Query in your theme, you can create a custom search template:

<?php
/**
 * Custom search template using Better_Search_Query
 *
 * @package YourTheme
 */

get_header();

// Parameters for Better_Search_Query.
$args = array(
    's'              => get_search_query(),
    'post_type'      => 'post',
    'posts_per_page' => 10,
);

// Create a new Better_Search_Query instance.
$better_search_query = new Better_Search_Query( $args );

// Get the search results.
$search_results = $better_search_query->get_posts();

if ( $search_results ) :
    echo '<div class="search-results">';

    foreach ( $search_results as $post ) :
        setup_postdata( $post );
        ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <header class="entry-header">
                <h2 class="entry-title">
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h2>
            </header>

            <div class="entry-summary">
                <?php the_excerpt(); ?>
            </div>

            <div class="search-relevance">
                <?php echo esc_html( $post->score ); ?> relevance
            </div>
        </article>
        <?php
    endforeach;

    wp_reset_postdata();
    echo '</div>';
else :
    ?>
    <p><?php esc_html_e( 'No results found. Please try different search terms.', 'your-theme-textdomain' ); ?></p>
    <?php
endif;

get_footer();

Advanced Configuration

For more control, you can configure Better_Search_Query with additional parameters. You can also post most WP_Query parametersOpens in a new window.

<?php
// Advanced parameters for Better_Search_Query.
$args = array(
    's'                      => get_search_query(), // This tells WP_Query that it is a search result.
    'post_type'              => array( 'post', 'page', 'product' ),
    'posts_per_page'         => 20,
    'orderby'                => 'relevance', // Order by relevance so recency can influence ranking.
    'order'                  => 'DESC', // Descending order of relevance.
    'boolean_mode'           => true, // Enable Boolean mode.
    'weight_recency'         => 50, // Favor newer results while preserving relevance.
    'recency_halflife'       => 180, // Decay period in days.
);

$better_search_query = new Better_Search_Query( $args );

Post statuses and caching

On the front end, a caller-supplied post_status is restricted to public statuses when the current user lacks edit_others_posts. Queries that include attachments may also use inherit. An empty selection falls back to publish, or to publish and inherit when attachments are included.

Admin searches retain WordPress’s own status and capability handling. The plugin’s result cache includes the effective post statuses after restriction, so requests with different allowed statuses do not share the same cache entry.

Advanced Features

Using Boolean Operators

Better Search supports MySQL’s Boolean mode for complex searches:

  • +word: Word must be present
  • -word: Word must not be present
  • >word: Increases relevance of word
  • <word: Decreases relevance of word
  • *: Wildcard for partial matching
  • "phrase": Exact phrase matching

Troubleshooting

No Results Found

If your searches return no results, check these common issues:

  1. Query Errors: See Debugging with Query Monitor to understand how to identify the Better Search Query.
  2. FULLTEXT Indexing: Ensure your database tables have FULLTEXT indexes enabled. You can recreate these in the Tools page.
  3. Post Types: Verify that the post types you’re searching for are included in your query parameters.

Performance Optimization

For large sites, consider these performance tips:

  1. Limit Search Fields: Include only the necessary fields.
  2. Cache Results: Implement a caching mechanism for search results. WP_Query now supports caching via the object cache.
  3. Paginate Results: Use pagination to limit the number of results per page.

Was this article helpful?