• Because Relevanssi overrides the search to include all custom fields, I can’t work out how to specifically filter to only the post title. I’ve created a filter on search for my music library website. What this allows is to do is the following, assuming my search query is: 1999?

    If I don’t filter to anything it produces all posts that has an association with 1999. 

    If I select year it only shows me posts where the value of the custom field ‘year’ is 1999 (all songs from 1999).

    If I select album it only shows me posts where the value of the custom field ‘album’ is 1999 (like Prince’s album). 

    So now, if I select Song Title, I’d expect only 1 result, and that should be the song 1999 by Prince. But that’s not happening. Song title is NOT a custom field. It’s the title of the post. How can I filter to it?

    Happy to share code if need be. Thank you!

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

    (@msaari)

    This code snippet makes Relevanssi only match titles:

    add_filter( 'relevanssi_match', function( $match ) {
        if ( $match->title < 1 ) {
            $match->weight = 0;
        }
        return $match;
    } );

    Now just make this trigger based on your song title filter – I suppose there’s a query variable you can check?

    Hello and thank you for sharing this filter to only match titles, it works fine for me.

    Now i need it to apply only in a specific page and i’m stuck…
    I tried this without success, the filter doesn’t seem to apply to the search result once the condition is set:

    add_filter( 'relevanssi_match', function( $match ) {
    	if ( is_page( 'search-products' ) ) {
    		if ( $match->title < 1 ) {
    			$match->weight = 0;
    		} 
    		return $match;
    	}
    } );

    To be noted: the search field is a WP Gridbuilder facet with Relevanssi enabled as the search engine.
    Not sure it makes a difference given the filter above works without the condition… but i’m far from mastering PHP.

    Any hint is welcome!

    Plugin Author Mikko Saari

    (@msaari)

    Is it an AJAX search? If you’re doing an AJAX search, you’re not on the page, so is_page() won’t work. You need a different condition to check. You can access the WP_Query object with global $wp_query; – perhaps the query variables in $wp_query->query_vars[] have something useful you can use as a condition? Another option is checking the parameters in $_REQUEST. I can’t be more specific, as I don’t know any of the details, but you can do this:

    add_filter( 'relevanssi_match', function( $match ) {
      global $wp_query;
      error_log( print_r( $wp_query->query_vars ) );
      return $match;
    } );

    Do a general search on your site and a search with your WP Gridbuilder facet, and compare the output that’s in the server error log. Can you spot a difference you can use as a condition?

    Also, return $match; needs to be outside the conditional, otherwise the search will break.

    Thanks a lot for the leads @msaari, now i will explore. ??

    Yes Ajax search is enabled on this WP Gridbuilder facet, probably a reason why the conditional fails by targeting a specific page as you mentioned.
    And yes, there’s an URL variable that could be used (generated by WPGB).

    • This reply was modified 1 year, 1 month ago by studioavanti.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Post Title Only?’ is closed to new replies.