0

I try to add a filter to make an elementor post widget displays the posts depending on a certain custom field.

The custom field key is adress and contains an array with some keys like street_number, street_name, city, post_code etc.

add_action( 'elementor/query/filterByPostCode', function( $query ) {
    
    if ('field' === $_GET['getby']) {
        
        $meta_query = $query->get('meta_query');

        if (!$meta_query) {
            $meta_query = [];
        }
        
        $meta_query[] = [
            'key' => 'adress',
            'value' => [ 
                'key' => 'post_code',
                'value' => $_GET['field'],
                'compare' => '='
            ],
        ];

        $query->set('meta_query', $meta_query);
    }

});

What will be the correct query ?

Thanks for help

1 Answer 1

0

I found the answer here. The query is simply :

        $meta_query[] = [
            'key' => 'adresse',
            'value' => $_GET['field'],
            'compare' => 'LIKE'
            ];

As it will search into the serialized (2D) array of the key adress in the table post_meta from the DB any occurence of my $_GET['field'] value. That means it will also search into every key name and its value, but that's enough for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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