Relevanssi - A Better Search
Custom Fields not working in wp_query as Search Parameter (6 posts)

  1. 3oh6
    Posted 3 years ago #

    This is a unique situation that I can't find any one else having.

    Relevanssi Free Version & WP 3.3.2

    I have a custom page setup with a wp_query using the Search Parameter (http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter). If I use the standard Search box I receive results from custom fields, titles, posts content, etc... Relevnassi works great.

    But using wp_query with the search parameter only returns results from the title & body of posts, AKA what WP Search would return without Relevanssi. It is as if Relevanssi only takes over the search box, and not wp_query with the search parameter.

    Is there a solution for this? Perhaps a feature that could be added. Thank-you in advance.


  2. Mikko Saari
    Plugin Author

    Posted 3 years ago #

    That is correct. The way you do it drops Relevanssi off the loop.

    What are you doing with the wp_query? There's probably a Relevanssi-compatible way to do it.

    The other way to do it is to pass a WP_Query object to relevanssi_do_query().

  3. 3oh6
    Posted 3 years ago #

    I am using wp_query on a couple "custom search results pages" for filtering categories through a series of drop down menu's, checkboxes, etc... I just also need to filter on search term so I was passing the "search box" as the search term to wp_query.

    I'll give relevanssi_do_query() a shot. That sounds like it should do the trick.

    Thanks for the reply.

  4. trinzia
    Posted 3 years ago #

    Could you please explain the solution?

    What did you put as your form action and where do you put relevanssi_do_query()?


  5. 3oh6
    Posted 3 years ago #

    Sorry, didn't see a reply. I still haven't tried getting the wp_query to work with relevanssi. At some point in the next couple weeks I likely will integrate it and will post back my findings.

  6. trinzia
    Posted 2 years ago #

    You might want these functions that msaari helped me write. I placed them in functions.php. Note that it would be better to use something like get_post_custom_keys, than to manually enter the field names, as I did. I haven't gotten around to that part yet.

     * RELEVANSSI function, to display custom fields in search results.
    add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3);
    function excerpt_function($content, $post, $query) {
    	$fields = array('meta_results', 'meta_inputs', 'meta_purpose', 'meta_special', 'meta_references');
    	$field_title = array('Results', 'Inputs', 'Purpose/Function', 'Special Characteristics', 'References');
    	foreach($fields as $key => $field){
    		$field_value = get_post_meta($post->ID, $field, TRUE);
    		$content .= $field_title[$key] . ": " . ( is_array($field_value) ? implode('<br/>', $field_value) : $field_value ) . " | " ;
    	return $content;
     *  Relevanssi function for advanced search form.  Names must be "s" and "e" for search and exclusion inputs.
    add_filter('query_vars', 'new_qvs');
    function new_qvs($qv) {
    $qv[] = 'e';
    return $qv;
    add_filter('relevanssi_modify_wp_query', 'exclusion_terms');
    function exclusion_terms($wp_query) {
         if (isset($wp_query->query_vars['e'])) {
             $negation = "";
             $e = explode(' ', $wp_query->query_vars['e']);
             foreach ($e as $exclusion_term) {
    			if (substr($exclusion_term, 0, 1) == '-') {
                     $negation .= ' ' . $exclusion_term;
                 else {
                     $negation .= ' -' . $exclusion_term;
             if ($negation != ' -') $wp_query->query_vars['s'] .= $negation;
         return $wp_query;

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Relevanssi - A Better Search
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic