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 !
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 !
Share Improve this question asked Jan 28, 2021 at 9:18 user3653409user3653409 153 bronze badges 1- 1 If the answer helped you to solve your issue, please mark it as accepted answer. – Antti Tolvanen Commented Jan 28, 2021 at 10:24
2 Answers
Reset to default 1You 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',
)
)
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.