0

I'm trying to query wordpress with multiple values but it looks like it can't take no more than 4 keywords

I'm currently using a loop and I already tried to switch to a IN operator but I need the query to work with wildcards and while REGEXP does the job, IN does not

foreach ($keywords as $value) {
    $args['meta_query'] = array(
        'relation' => 'OR',
        array(
            'key' => 'customfields',
            'value' => $value,
            'compare' => 'REGEXP'  
        )
    );
}

In this case $keywords is an array of words that I need to query to a custom field. It works with a few keywords, but it breaks with 5 or more.

2 Answers 2

0

Have you tried passing an array of values to the value argument?

$your_terms = array( 'foo', 'bar', 'baz' );

$args['meta_query'] = array(
    'relation' => 'OR',
    array(
        'key' => 'customfields',
        'value' => $your_terms,
        'compare' => 'REGEXP'  
    )
);

$this_query = new WP_Query( $args );
3
  • Did you mean to delete foreach loop aswell? Anyway this just doesn't work... Jun 10, 2019 at 15:30
  • What does "this just doesn't work" mean? WP_Query does not require a loop to iterate numerous queries that can be accomplished with a single, properly constructed one.
    – jdm2112
    Jun 10, 2019 at 15:40
  • I don't get any result by doing so, my code gets the desired results instead, mine just doesn't work with more than 4 keywords (more than 4 iteration) Jun 10, 2019 at 16:06
0

In its current form, your meta_query is overwritten in each iteration. In the final effect, you have only one custom field in meta_query:

Depending on the operator you want to use, meta_query argument should look as follow:

  • for "LIKE", "REGEXP"

    if( is_array($keywords) && count($keywords) ) {
    
        $args['meta_query'] = array( 'relation' => 'OR' );
        foreach( $keywords as $value )
        {
            $args['meta_query'][] = array(
                'key' => 'customfields',
                'value' => $value,
                'compare' => 'REGEXP', // or 'LIKE'
            );
        }
    }
    //  === result: ===
    // $args['meta_query'] = array( 
    //     'relation' => 'OR',
    //     [ 'key' => 'customfields', 'value' => 'value_1', 'compare' => 'REGEXP' ],
    //     [ 'key' => 'customfields', 'value' => 'value_2', 'compare' => 'REGEXP' ],
    //     [ 'key' => 'customfields', 'value' => 'value_3', 'compare' => 'REGEXP' ],
    // );
    
  • for "IN"

    if( is_array($keywords) && count($keywords) ) {
    
        $args['meta_query'] = array( 
            'relation' => 'OR',
            array(
                'key' => 'customfields',
                'value' => $keywords,
                'compare' => 'IN',
            )
        );
    }
    
4
  • thanks for helping: while the IN query does not return any results (maybe IN operator hasn't %wildcards% for words inside the array?), the first one works just fine as the one I posted but, like mine, if the array is equal or greater than 5 words it takes a lot for the page to load (minutes). Jun 11, 2019 at 19:21
  • IN looks for the exact match. The meta query is expensive, especially when using LIKE, that's why it's slow.
    – nmr
    Jun 11, 2019 at 21:17
  • so what's a possible solution to this? how can I query for more than 4 terms without going that slow? Jun 12, 2019 at 0:08
  • Slow meta query with multi meta keys
    – nmr
    Jun 12, 2019 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.