Problem : WP_query using meta_query for an ACF checkbox field
I’m using WP_Query to show a list of results. I want to filter these based on an ACF checkbox field. I think that I’m getting muddled up with how to correctly parse the ‘value’ array from my ACF field.
Below solution helped me.
Soution code for : WP_query using meta_query for an ACF checkbox field
// Get the selected options
$my_acf_checkbox_field_arr = get_field('my_checkbox'); // OR $my_acf_checkbox_field_arr =array('checkbox-value1','checkbox-value2');
// Build the meta query based on the selected options
$meta_query = array('relation' => 'OR');
foreach( $my_acf_checkbox_field_arr as $item ){
$meta_query[] = array(
'key' => 'checkbox',
'value' => $item,
'compare' => 'LIKE',
);
}
// args
$args = array(
'numberposts' => -1,
'post_type' => 'my-cpt',
'meta_query' => $meta_query,
);
$the_query = new WP_Query( $args );
No comments:
Post a Comment