Using get_posts to get posts based on a checkbox value with Advanced Custom Fields

I have a custom post type, “Jobs”.

These jobs have an ACF checkbox field, “city”. “Jobs” can have more than one “city,” so the jobs returned have meta values look something like this:

[city] => Array
(
[0] => Austin
)

[city] => Array
(
[0] => [Austin]
[1] => [Boulder]
)

[city] => Array
(
[0] => [Seattle]
[1] => [Portland]
[2] => [Boulder]
)

I need to write a get_posts query so that I can return only jobs that match a single city.

However, I can’t figure out what my arguments need to look like, since I need to match the value of “meta_value” is an array.

Here’s my code, can you guys help?

function get_city_jobs() {
$args = array(
‘post_type’ => ‘careers_post_type’,
‘numberposts’ => -1,
‘meta_key’ => ‘city’,
‘meta_value’ => ‘Boulder’,
‘meta_compare’ => ‘LIKE’
);

$jobs = get_posts($args);

//daveDebug($jobs);

foreach ($jobs as $job) {
echo “<pre>”;
print_r( get_fields($job->ID));
echo “</pre>”;
}

}

Read more here:: Using get_posts to get posts based on a checkbox value with Advanced Custom Fields

Leave a Reply

Your email address will not be published. Required fields are marked *