Home › Forums › General Issues › Serialized values v/s Multiple meta entries for checkboxes › Reply To: Serialized values v/s Multiple meta entries for checkboxes
You are correct that using LIKE rather than IN will mean many lots of joins and will reduce the performance of your query. I cannot say why serialized arrays were chosen for storing some values (I’m not the developer), but it has always been this way for any field that can store multiple values. Either it was easier to code this way or there was some other reason. At a guess it’s because 99% of the time the general WP population won’t be doing custom queries and will simply be showing the contents of the fields in some way. Other reason might include things like the true/false field which is also really a checkbox field that only stores one value and it may make it easier to share code from these field type.
It is possible to work around this by taking the values from the checkbox field and putting them into standard WP storage format by using an acf/update_value filter.
add_filter('acf/update_value/name=your_checkbox_field', 'convert_acf_checkbox', 10, 3);
function convert_acf_checkbox($value, $post_id, $field) {
// the place we'll store the content of the acf checkbox
$new_field = 'converted_checkbox_field';
// delete anything that's already in the field
delete_post_meta($post_id, $new_field);
if (!is_array($value) || !count($value)) {
// nothing selected
return $value;
}
foreach ($value as $meta_value) {
add_post_meta($post_id, $new_field, $meta_value);
}
return $value;
}
With this in place you can use the new field name with IN.
Another choice is to create a new checkbox field type that stores data with multiple entries, this is what the developer has suggested when this question has been asked before, which makes me think he’s not thinking about changing it. https://www.advancedcustomfields.com/resources/creating-a-new-field-type/
EDIT: code edited to correct typo/error mentioned below
Back to my original solution, I’ve actually created an entire plugin dedicated to converting these types of fields and can be used to covert the data in previously saved posts. It hasn’t gotten much testing from anyone other than myself so it could still have some bugs in it. For a one off field I would create the filter like I said above and just use the plugin to get any existing post data converted over. https://github.com/Hube2/custom-field-converter
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.