Filter on a ACF field doesn't work

Hello,

I’m creating a website that displays a list of airport (1 page = 1 airport) and a list of country (1 country page list airports in the country.

On my aiport page, I added an Block Element on the sidebar. I want the Element to display a random list of airport in the same country, e.g., I’m viewing JFK airport and the sidebar is showing Atlanta ATL, Chicago ORD, etc.

I set up the query loop so it displayed the airports, but when I added the filter (with additional CSS class on grid) it didn’t work. It just shows a list of airports ordered by published date.

Here’s the filter code I added " Code Snippets":

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attribute ) {
	if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'airport-template-widget-same-country' ) !== false ) {
		$query_args['meta_query'] = array(
            array(
                'key' => 'country_fr',
                'value' => get_field('country_fr'),
            ),
        );
		$query_args['orderby'] = 'rand';
    }

    return $query_args;
}, 10, 2 );

And here’s some screenshots of the configuration.

The sidebar:

ACF field on specific page:

ACF configuration:

I don’t understand why it doesn’t work and the filter isn’t applied to the query loop.

(i tried using Country instead of country_fr on “Key” but doesn’t change anything)

Someone has an idea ?

Thank you.

Hi ! Anyone has an idea of what I’m doing wrong?

Hi @michaelseo

Can you try setting the value to something static first to test?

Try adding this inside the meta_query as well:

'compare' => '='

or

'compare' => 'LIKE'

Thank you for your help.

A dev helped find a solution, that was :

	if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'airport-template-widget-same-country' ) !== false ) {
		$query_args['meta_query'] = array(
            array(
                'key' => 'country_fr',
                'value' => get_field('country_fr'),
            ),
        );
		$query_args['orderby'] = 'rand';
		$query_args['post__not_in'] = [get_the_ID()];
		
    }
   	return $query_args;
	
}, 10, 2 );

I see. Glad you got it working!