Performing search operations is important for users to get the most optimized results so they perform fewer actions and reach to the desired results. One of the ways to provide this optimized search on WordPress is to do searching only based on the post titles using wp_query search titles. This is because most of the time, the user search keyword is related to the post titles, in turn, giving out the most desired results.

To perform only the post title search we need to create or modify the theme’s functions.php i,e using wp_query search titles with the following code.

function wcs_search_by_title_only( $search, &$wp_query ) { 

        global $wpdb;

        if ( empty( $search ) )

            return $search;

        $q = $wp_query->query_vars;

        $n = ! empty( $q['exact'] ) ? '' : '%';

        $search =

        $searchand = '';

        foreach ( (array) $q['search_terms'] as $term ) {

            $term = esc_sql( like_escape( $term ) );

            $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";

            $searchand = ' AND ';

        }

        if ( ! empty( $search ) ) {

            $search = " AND ({$search}) ";

            if ( ! is_user_logged_in() )

                $search .= " AND ($wpdb->posts.post_password = '') ";

        }

        return $search;

}

add_filter( 'posts_search', 'wcs_search_by_title_only', 500, 2 );

Adding this code snippet (wp_query search titles) in your functions.php will first check if the search is returning a result or not. If it is returning any result then, it will perform an SQL query which will try matching the user keywords with post title and return the matched results.

 

WordPress SnippetsAssign Project WordPress

Though there are many ways to search using the titles, an easier way to do this is by using WP fastest site search plugin by Expertrec

  1. Install WP fastest site search plugin from WordPress plugin store.
  2. Go to https://cse.expertrec.com/newuser?platform=wordpress.
  3. Enter your WordPress website URL and click on next->
  4. The crawl will start in the background.
  5. Login to your WordPress admin panel.
  6. In your left panel you must be able to see expertrec’s WordPress plugin, click on that.
  7. Get your site ID (or) API key from your expertrec control panel ( here ).
  8. Copy paste your site id in your WordPress control panel and click on save changes.
  9. Make sure you have upgraded to a paid plan before 15 days of free trial. (Price begins at 9 USD per month)
  10. That’s it! Your brand new WordPress search is up and running.

Add great search to your wordpress site at 9 USD per month

 

 

Watch this video to learn more about having all  posts on one page

You may also like

Black Friday sale ends in 0 Days 23 Hours 33 Minutes 49 Seconds Get your free search now!
Sign up