Posted on

SearchWP Live Ajax Search

Relevanssi doesn’t ship with a live search or autocomplete feature that would display results as the user types the search terms. There are many other plugins that provide this functionality, but few of these plugins work with Relevanssi. SearchWP Live Ajax Search is the best one that does. It’s very easy to use, most of the time just install and go (it doesn’t even have a settings page!).

Update January 2022: Use Relevanssi Live Ajax Search instead! It requires none of these fixes and has other enhancements for Relevanssi users.

Attachments

Sometimes the default settings are not good enough, though. One example is searching for attachments. By default, SearchWP Live Ajax Search doesn’t include any attachments in the search results. The post type is not a problem, but the post status is: SearchWP Live Ajax Search includes only posts with the post status publish, while attachments have a status of inherit.

This is easy to fix. Just add the following functions to your site:

add_filter( 'searchwp_live_search_query_args', 'fix_searchwp_query_args' );
function fix_searchwp_query_args( $args ) {
	$args['post_status'] = 'inherit,publish';
	return $args;
}

This will set the post_status parameter to include posts with inherit, allowing attachments. You can also add private to the list to include private posts for the users who are allowed to see them.

Just removing the post_status parameter will include drafts in the results. Normally drafts shouldn’t appear in searches, but since the SearchWP Live Ajax Search is an AJAX search, it is done in admin context, where drafts are allowed.

Users and taxonomy terms

In order to see users and taxonomy terms in the search results, you also need to adjust the post_type parameter so that either users are included or nothing is excluded. In most cases you can just remove the post_type parameter completely and leave the post type handling to Relevanssi:

add_filter( 'searchwp_live_search_query_args', 'fix_searchwp_query_args' );
function fix_searchwp_query_args( $args ) {
	unset( $args['post_status'] );
	unset( $args['post_type'] );
	return $args;
}

Users and taxonomy terms need you to blank out the post_status parameter, which includes drafts in the results. There’s also another problem: the default template in SearchWP Live Ajax displays the post type’s name. For users, that’s not going to work, and will show empty parentheses and cause an error message in the console.

You can fix both of these problems with a new template. Take this template and save it as searchwp-live-ajax-search/search-results.php under your theme. This will fix the post type name and will skip the drafts in the results.

13 comments SearchWP Live Ajax Search

  1. Is it possible to open the SearchWP Live Ajax Search results in a new page? I mean when actually clicking on a live search result link to open in a new window.

      1. Aww shucks.. thanks for the reply anyway. I guess I should have checked the restrictions before starting to add the links.

  2. Hey! Does AJAX live search work if a user types a custom field and relevanssi has built an index including custom fields? If so, what am I doing incorrectly? AJAX returns no results were found but if you click on the search icon you get correct results.

    1. Christophoros, are you using SearchWP Live Ajax Search or Relevanssi Live Ajax Search? Both do use Relevanssi, so if Relevanssi finds the custom field content, the AJAX search should do so as well.

      If you are using some third-party AJAX search, it’s probably just not using Relevanssi to power the search. Whether it can be fixed or not depends on which search you’re using.

      1. Hello again. I am using Relevanssi Live Ajax Search. So from what I understand Relevanssi finds the custom because once I click on the search button it returns the correct result but AJAX does not find it because I can’t see it live and I get No results were found! instead.

        Check it yourself by typing this code 133702969 to the search bar of the website I’ve attached you to the form. You will see no results were found but when you click on the search icon you will be redirected to the correct product.

        1. Christophoros, you may have Relevanssi Live Ajax Search installed, but your search form is not using it. Your search form is using some Elementor AJAX search, which is not using Relevanssi. The search form input field is missing the Relevanssi Live Ajax Search activation codes, it has been built in a way where the automatic takeover mechanism in Relevanssi Live Ajax Search does not work.

          I’m not sure what’s the best way to solve this in Elementor, but the easiest way is to probably use a different search form element, either one that Relevanssi Live Ajax Search can edit automatically, or a custom one where you can make the required modifications yourself.

Leave a Reply to Christophoros Cancel reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published.