Search code examples
wordpressadvanced-custom-fields

Wordpress search with ACF fields all words


im building a custom search with multiple ACF fields. I need to search for all words in search input and in all ACF fields, so i wrote the query like this:

 $keywords = explode(' ', $_POST['search_value']);

    $searchable_fields = array('acf_name', 'acf_surname', 'street');


    $meta_query = array();
    foreach ($keywords as $keyword) {
        foreach ($searchable_fields as $searchable_field) {

            $meta_query[] = array(
                'key' => $searchable_field,
                'value' => $keyword,
                'compare' => 'LIKE',
            );
        }
    }


    $meta_query = array(
        'relation' => 'OR',
        $meta_query
    );

    $args = array(
        'post_type' => "custom_post_type",
        'posts_per_page' => -1,
        'meta_query' => $meta_query
    );

But this code is not working. If for example search for: "Wilhelm SCHROFF" and print_r for $args, i have:

[meta_query] => Array
    (
        [relation] => OR
        [0] => Array
            (
                [0] => Array
                    (
                        [key] => stolpersteine_name
                        [value] => Wilhelm
                        [compare] => LIKE
                    )

                [1] => Array
                    (
                        [key] => stolpersteine_surname
                        [value] => Wilhelm
                        [compare] => LIKE
                    )

                [2] => Array
                    (
                        [key] => street
                        [value] => Wilhelm
                        [compare] => LIKE
                    )

                [3] => Array
                    (
                        [key] => stolpersteine_name
                        [value] => SCHROFF
                        [compare] => LIKE
                    )

                [4] => Array
                    (
                        [key] => stolpersteine_surname
                        [value] => SCHROFF
                        [compare] => LIKE
                    )

              ecc, ecc

            )

    )

I think the problem its in the $meta_query array. How can i solve this? Thanks


Solution

  • There is a problem while adding 'relation' => 'OR' to $meta_query. The way you did this $meta_query lands in an additional array. Try this:

    $keywords = explode(' ', $_POST['search_value']);
    $searchable_fields = array('acf_name', 'acf_surname', 'street');
    $meta_query = array('relation' => 'OR');
    
    foreach ($keywords as $keyword) {
        foreach ($searchable_fields as $searchable_field) {
            $meta_query[] = array(
                'key' => $searchable_field,
                'value' => $keyword,
                'compare' => 'LIKE',
            );
        }
    }
    
    $args = array(
        'post_type' => "custom_post_type",
        'posts_per_page' => -1,
        'meta_query' => $meta_query
    );