• I see in the documentation you can add weight based on custom field but it’s not clear to me how to do the following:

    I have an ACF custom field called “author_search”. If the search term matches the text in “author_search” i want that result to have extra weight.

    There will be multiple authors in the “author_search” custom field. An example value is “john smith jane doe” … no commas. So searching for John Smith or Jane Doe should add extra weight to that result.

    Thanks!

    The page I need help with: [log in to see the link]

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

    (@msaari)

    This would be easier with Relevanssi Premium. With the free version, the solutions using the relevanssi_match hook require you to check the custom field for each post, which adds many extra database calls.

    Here’s a solution that only makes one extra database call:

    add_filter( 'relevanssi_results', 'boost_author_search' );
    function boost_author_search( $results ) {
        $query = get_search_query();
        $authors = relevanssi_get_post_meta_for_all_posts( array_keys( $results ), 'author_search', true );
        array_walk(
            $authors,
            function( $value, $post_id ) use ( &$results, $query ) {
                if ( stripos( $value, $query ) !== false ) {
                    $results[ $post_id ] *= 100;
                }
            }
        );
        return $results;
    }

    If the search term is found in the author_search field, the weight for the post is multiplied by 100.

    • This reply was modified 1 year, 8 months ago by Mikko Saari.
    Plugin Author Mikko Saari

    (@msaari)

    Ah, since you mentioned in the other thread you’re using Search & Filter, I’m not sure if get_search_query() works with that… but S&F support can help you figure out a way to access the search query if necessary.

    This may also work:

    global $wp_query;
    $query = $wp_query->query_vars['_sf_s'];
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Give extra weight if a search matches a certain custom field’ is closed to new replies.