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 !
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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' => '^[email protected]',
'compare' => 'REGEXP',
)
)
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0