• Anonymous User 17275278

    (@anonymized-17275278)


    I have a few custom fields (with ACF) to search through but default behavior will look for %phrase match%, what I need is an AND operator between terms, like this %phrase% AND %match%, just like the default WordPress search parameter (but with and operator).

    I tried exploding the string by spaces and looping through the meta_query and actually it works but it results in a very heavy query that will take minutes to load the page with more than 4/5 terms in it which is not acceptable.

    What would be a possible solution? Is there a relevanssi feature/workaround for this?

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

    (@msaari)

    If you index the custom fields with Relevanssi, Relevanssi will search them the same way it search post content. If you set Relevanssi to use AND operator, you should get the results you expect. Relevanssi won’t search custom fields as a phrase, unless you make it a phrase search yourself.

    So if you set Relevanssi to index the custom fields and set the default operator to AND, you should get the results you need, if I’ve understood you correctly.

    Thread Starter Anonymous User 17275278

    (@anonymized-17275278)

    You are correct but I have an advanced search mask in which I have a “free search” that looks for every visible custom field which already uses the s parameter (this works perfectly) and then I have different fields, in the same form, which should look into specific fields.

    I would likely need multiple s parameters but I don’t think that’s possible so, as said before, I tried looping into the meta_query which actually works but it’s very very heavy and it will take minutes with more than 4/5 or more keywords

    The final result should work like this:

    free search (every custom field) AND specific_field_1 (A few specific custom fields) AND specific_field_2 (A few more specific custom fields)

    And so on…
    Hope this is clear, thank you

    Plugin Author Mikko Saari

    (@msaari)

    Relevanssi can digest complicated meta_query arrays, so if you can formulate your needs into a meta_query, that will do it.

    If you can’t create a meta_query out of your query, then one solution is to search everything that matches at least part of it, and then add a relevanssi_hits_filter filter hook that filters out the unwanted posts. If done well, that’s not particularly heavy or slow.

    Thread Starter Anonymous User 17275278

    (@anonymized-17275278)

    This is the meta_query that I’m trying to use, it does its job but it’s very very heavy if you input more than 4/5/6 keywords, the more keywords you input the worst it gets until you get a timeout:

       $args['meta_query']['relation'] => 'AND';
    
        $inputvalue = "new shilling toy book ward lock tyler"; //keywords
        $inputvalue = explode(" ", $inputvalue); //split into single terms
        
        foreach ($inputvalue as $searchterm) {
            $args['meta_query'][] = array(
                'relation' => 'OR',
                array(
                    'key' => 'series_$_series-name',
                    'value' => $searchterm,
                    'compare' => 'LIKE'
                ),
                array(
                    'key' => 'series_$_series-number',
                    'value' => $searchterm,
                    'compare' => 'LIKE'
                ),
            );
        }

    the meta_query logic is right: series-name or series-number (first keyword) AND series-name or series number (second keyword) and so on…
    I can’t use IN operator because I need every single term to be searched through wildcards. e.g. %shilling%

    Don’t worry about dollar signs, they are replaced with relevanssi_where with %s, it works properly.

    Plugin Author Mikko Saari

    (@msaari)

    Yeah, the resulting MySQL query becomes very complicated quickly, and those LIKE queries will be slow. In that case, I think the best solution is the relevanssi_hits_filter route: get all posts, then loop through them and discard the ones that don’t match your meta query restrictions. Checking the meta fields of individual posts may well be faster than doing a complex restriction query.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get meta_query terms to work like default search term’ is closed to new replies.