Question

I want to display a Custom post Type "Companies" filtered by zipcode.

In my CPT I have an ACF field called 'companies_zip_code', their values looks like this :

29200
29860
35000
22350
...

I want to display only companies where their companies_zip_code begins with 29

So I write this WP_QUery :

$args = array(
    'post_type' => 'companies',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'company_address_zip_code',
            'value' => 29,
            'compare' => 'LIKE',
        )
     )
);

$custom_query = new WP_Query($args);

With that I want to display only companies with zipcodes like this : 29200,29500,29860 etc.. But it does not work

Thanks for your help !

Was it helpful?

Solution

You could try to use the REGEX compare option for the meta_query, like so:

 'meta_query' => array(
        array(
            'key'       => 'company_address_zip_code',
            'value'     => '^29@',
            'compare'   => 'REGEXP',
        )
    )

OTHER TIPS

In meta_query Change:

    'value' => 29,
    'compare' => 'LIKE',

to

    'value' => array(29000, 29999),
    'compare' => 'BETWEEN',

Change two numbers in value accordingly to range of zip codes you want.

More info can be found on this thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top