Home Forums General Issues Serialized values v/s Multiple meta entries for checkboxes


Serialized values v/s Multiple meta entries for checkboxes

  • Just a general question/discussion to learn.
    Why are serialized arrays being used over multiple meta entries for checkbox?

    For checkbox fields it would be easier to use an IN query.!9/f06589/1

    I’ve noticed that the use of LIKE was suggested to search through fields like this in other threads, but I think the IN queries would perform better compared to LIKE, right?

    Is there any better way to filter than using ‘LIKE’?
    e.g. What if the checkbox has 10 possible choices and I wanted to match 7 of those fields? (I think that would translate to 7 more JOINs when using the ‘LIKE’).

  • 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.

    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.

  • Thanks 🙂
    I’ve been trying out your plugin and its great.
    Btw what does the related post thing do?

  • It will let you pull data from related posts that are created using either a relationship field or a post object field and store it as part of the post so that you can search it like it was part of the post. I guess it’s hard to explain. Let’s say that you have a products post type and you also have a product specs post type because the same product specs can be used on more than one product. In WP it is virtually impossible to search for posts that have a relationship to another post that has a specific value in a custom field. Actually this is the reason that I built that feature. Using it I can copy the values from the post meta for the spec post into fields for the product post. Yes it clutters up the database with duplicate information but it makes it possible to use WP_Query to search the products posts for that information.

  • Cool 🙂 I don’t have a need to that atm but its good to know what it does 🙂

  • I need some help understanding what to fill up in the forms.. What does the ‘Pointer field’ and ‘Fields’ refer to? Screenshot :

    Suppose I have custom post type CPT_A with custom field CF_A which is a relationship field type.
    I need to update/sync the custom field CF_B of another custom post type CPT_B ..
    How do I go about it?

  • I am, but if the question is about the field converter it would be better if you ask questions over on GitHub

  • Sure, as it was a question and not an issue I asked it here.

    Link to the issue on github if someone stumbles upon this later.

  • Hi john i used your code but wordpress returned an error i fixed in by using
    if ((!is_array($value)) || (!count($value))) {
    instead of if (!is_array($value)) || !count($value)) {
    in your code …problem was caused due parantheses … and thank you for your code it saved my time and life 🙂

  • I’ve corrected the code above. Thanks @mam_magician

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Serialized values v/s Multiple meta entries for checkboxes’ is closed to new replies.