Support » Plugin: Relevanssi - A Better Search » [Plugin: Relevanssi – A Better Search] Custom Fields not working in wp_query as Search Parameter

  • 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.

    http://wordpress.org/extend/plugins/relevanssi/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mikko Saari

    (@msaari)

    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().

    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.

    Could you please explain the solution?

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

    Thanks.

    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.

    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;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: Relevanssi – A Better Search] Custom Fields not working in wp_query as Search Parameter’ is closed to new replies.