Direct access to the query engine

Relevanssi can’t be used in any situation, because it checks the presence of search with the is_search() function. This causes some unfortunate limitations and reduces the general usability of the plugin.

You can, however, access the query engine directly. There’s a function called relevanssi_do_query(), which can be used to do search queries just about anywhere. The function takes a WP_Query object as a parameter, so you need to store all the search parameters in the object (for example, put the search terms in $your_query_object->query_vars['s']). Then just pass the WP_Query object to Relevanssi with

relevanssi_do_query($your_query_object);.

Relevanssi will process the query and insert the found posts as $your_query_object->posts. The query object is passed as reference and modified directly, so there’s no return value. The posts array will contain all results that are found.

  • Andrei

    I suggest that you add this note to your installation notes on the plugin page. Something like “If you develop your own search form, use this function to access Relevanssi directly”.

    It would have saved me at least an hour :) Otherwise, great work!

    • http://www.mikkosaari.fi/ Mikko Saari

      Good point, I’ll fix that.

  • cirkut

    I’m just wondering. When you say to pass the search terms into the query object, is there any additional step that needs to be done rather than `$search = new WP_Query($search_query);`? `$search_query` has `’s'` set, so do I just call `relevanssi_do_query($search)`?

    • http://www.mikkosaari.fi/ Mikko Saari

      Yes, that should work.

      • Chris

        It doesn’t work. I am doing a custom AJAX search and I can’t get any results with your plugin. Can you please post a complete example on how to query the engine?

        [code]
        $your_query_object->query_vars['s'];
        relevanssi_do_query($your_wp_query_object);
        print_r($your_query_object);
        [/code]

        $your_query_object->query_vars['s']; does not read the querystring. I have tried with other strings, but with the same result.

        $wp_query does not contain anything for posts, query or s either.

        • http://www.mikkosaari.fi/ Mikko Saari

          Do note the example has a mistake in the variable names, it’s $your_query_object in one place and $your_wp_query_object in another. It should be the same everywhere.

          I don’t have a complete example ready, as I don’t use this feature on any of my blogs.

          • Chris

            Yeah sorry for the variable names, they should be the same. Nevertheless, it’s not working and I don’t know why. Only thing I have ever managed to get is errors from the relevanssi.php file.

        • Erwin

          Late reply, for future reference: Chris, I think the issue is the fact that you’re doing an custom AJAX search here, and not a particular problem with Relevanssi. You’re calling a PHP file directly – but that means that nothing WordPress is loaded in that file.

          My suggestion: create your AJAX search php file as a custom template, assign this template to a post and then call the post URL rather than the php file directly. Your PHP file doesn’t need to contain anything extra, but since it’s loaded through WordPress will contain all the necessary functionality.

  • wp_fish

    I’ve created a wordpress site but have added my own table which also has a title and description field. Is it possible to include this new table in the search results?

    • http://www.mikkosaari.fi/ Mikko Saari

      No, that’s not possible (not yet, at least).

  • http://www.mikkosaari.fi/ Mikko Saari

    @Chris, if you do have a license that includes support, drop me an email at mikko@relevanssi.com and show me the complete code and I’ll take a look at it.

  • Morten

    Hi

    I manage to use you relevanssi_do_query to only search in my custom post, but you do not use the result of the Wp_Query in your search just some of the query variables ?

    I would like to use the meta_query to only search for some key values. Is this possible in the licensed version ?

    • http://www.mikkosaari.fi/ Mikko Saari

      Please explain in more detail what you want to do, I don’t quite understand what you’re after here.

  • Dmitry

    Hi,
    how about presswork + relevanssi? Doesn’t work for me…
    In readme file there is very poorly explained how get relevanssi to work…

    I use this piece of code for my search form:

    if(!function_exists(‘pw_html5_search_form’)) :
    function pw_html5_search_form( $form = false ) {
    $form = ‘
    ‘ . __(‘Search for:’, “presswork”) . ‘

    ‘;

    return $form;
    }
    endif;

    Note: home_url( ‘/’ ) . ‘search
    I tried with search page and post types. Bot aren’t working…
    Any ideas?

    Best regards.

    • http://www.mikkosaari.fi/ Mikko Saari

      I’ve never even heard of PressWork, so hard to say… What does it even do?

      • Dmitry

        Yeah, it’s a theme engine.
        Pretty good one: http://presswork.me
        I use it nowadays in every project.

        In my child theme is something like this (inside of index.php):
        ID, ‘pw_single_layout’, true);
        if($fullwidth==2) $layout = “maincontent”;
        }
        $layout = explode(“,”, $layout);
        $i = 1;
        foreach($layout as $elem) {
        pw_get_element($elem, “el”.$i);
        $i++;
        }
        ?>

  • Drew

    I’m building a page that pulls in a group of posts based off a custom field. Now that I have this group of posts I want to use Relevanssi to search JUST from this group using terms set by the theme (not the user). Is this possible with the premium version? If not do you think it’s possible at all? Perhaps we can hire you to make the changes? Thanks!

    • http://www.mikkosaari.fi/ Mikko Saari

      If the group is based off a custom field, it should be fairly easy to do a relevanssi_hits_filter filter function that’ll restrict the search to only the posts you want.

      • Drew

        Great, thanks for the tip!

  • Max Hodges

    is there a Relevanssi wiki? I’d like to contribute an article.

    Here’s how to use relevanssi in plaec of keyword searches using wp_query:

    $wrx_search_txt = get_query_var('s');
     
     if (!empty($wre_search_txt))
        {	global $wp_query;
    		$rq = new WP_Query('s='.$wre_search_txt);
    		$rq->query_vars = $wp_query->query_vars;
    		relevanssi_do_query($rq);
         }

    then use $wp_query to get the posts i.e.

     have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();
  • http://www.mikkosaari.fi/ Mikko Saari

    No wiki, but contributions to knowledge base and the user manual are certainly welcome.

  • web559

    I have a custom WP_Query in a post type search page which returns results with Relevanssi (Premium) off, but returns no results with Relevanssi on.

    Max Hodge’s code didn’t solve it for me, nor did wrapping $wp_query in relevanssi_do_query(). $wp_query->query_vars['s'] is already set, and is_search() evaluates to true. Relevanssi works fine on the main search page, just won’t work in this page template. There are no calls to query_posts() in the page or the widgets, though I do clone the query at the top ($temp_query = clone $wp_query;) if that matters.

    Any idea what might be going on? (I would even be fine completely disabling Relevanssi on this page.)

    • http://www.mikkosaari.fi/ Mikko Saari

      If $wp_query->query_vars['posts_per_page'] is not set, the search will not return results. That’s one thing you can check.

      Other than that it’s hard to say without seeing your page template.

  • web559

    I have removed everything else from the template, and the same thing happens—specifying any value for ‘s’ results in zero results.

    Here is the full template I am testing with: https://gist.github.com/3903803 and here is a dump of the WP_Query object: https://gist.github.com/3903820

    This is a post type archive page (—.com/posttype?s=test). The default search page works fine (—.com/?s=test). I’m perplexed.

    • http://www.mikkosaari.fi/ Mikko Saari

      As am I, I don’t know why this is happening. Very hard to say without taking a closer look – I’d debug this by checking what’s happening inside Relevanssi when it’s processing the query, adding var_dumps() here and there.

  • sk

    Hey web559 — did you ever solve this? I’m having the same issue.

    • web559

      Not yet, unfortunately, but I will post an update here if/when I do.

  • Umesh

    Hi Relevanssi team,

    Any update on this thread?

    Thanks!

    • http://www.mikkosaari.fi/ Mikko Saari

      No, no idea.