1

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 !

1
  • 1
    If the answer helped you to solve your issue, please mark it as accepted answer. Commented Jan 28, 2021 at 10:24

2 Answers 2

1

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',
        )
    )
1
  • Hi ! The Regex does not work, it returns nothing Commented Jan 28, 2021 at 9:43
1

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.

Your Answer

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

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