Support

Account

Home Forums ACF PRO Map/post ACF field to meta_key value where meta_key field has multiple values

Solved

Map/post ACF field to meta_key value where meta_key field has multiple values

  • Hi there,

    I’m using a plugin that saves meta_key vales for a custom post type, where the meta_key can have multiple instances/values. I’m using acf_form to edit my custom post type and am unsure how to map/post ACF fields to this one meta_key in a way that creates/updates these multiple instances without overwriting the other ones. I’ll to my best to explain…

    For instance, my custom post type can have:

    meta_key = _my_cpt_field

    _my_cpt_field = 1
    _my_cpt_field = 2
    _my_cpt_field = 3

    Accordingly, via get_post_meta() I can get the field values like:

    $my_cpt_field = get_post_meta($post_id,'_my_cpt_field',false);
    
    $my_cpt_field[0];
    $my_cpt_field[1];
    $my_cpt_field[2];

    I’m using acf_form() to edit my custom post type and I would like to know how I can save ACF fields to map to the respective meta_key, with separate values.

    For instance, when a user clicks the submit button on the acf_form, I want the following to happen:

    my_acf_field_1 — maps/posts to –> $my_cpt_field[0];
    my_acf_field_2 — maps/posts to –> $my_cpt_field[1];
    my_acf_field_3 — map/postss to –> $my_cpt_field[2];

    So, my questions, do I need to use update_field, acf/save_post, or something else? How would this work in conjunction with acf_form?

    Huge thanks in advance and I’m happy to clarify more if need be!

  • This is going to depend on how the fields are stored in the database by the other plugin. Are they saved on separate database rows like standard postmeta in WP? Or is the value stored as a serialized array in the database?

    The tircky part is not getting the data from the other plugin and inserting it into ACF. The trick is putting back into the db in a form that the other plugin can use.

    What type of field are you using in ACF? Since the other plugin allows multiple values I’m assuming that you can add more values. Are you using a repeater field in ACF?

  • Hey John!

    Thanks for the response!

    RE: Database Values The value in question uses separate database rows.

    You’re right about getting the data from the other plugin (the plugin is CPT-onomies, https://wordpress.org/plugins/cpt-onomies/) being relatively easy and I’m able to do this.

    RE: Type of ACF Field

    I’m using a select that I’m populating dynamically. Ideally each one of these selects would correspond to a different row in the database meta_key value.

    Hope this makes sense and I’m happy to clarify with anything else if need be!

  • Using a select that allows multiple values is a good choice it holds arrays in the database and it’s probably the easiest field to deal with. ACF stores these fields as serialized array.

    You already have the values populated into the acf field by your comment, so putting them back into the other fields you would do something like this.

    
    add_action('acf/save_post' 'update_cptonomies_field_name');
    function update_cptonomies_field_name($post_id) {
      $values = get_field('acf_field_name', $post_id);
      delete_post_meta($post_id, 'cptonomies_field_name');
      if ($values) {
        foreach ($values as $value) {
          add_post_meta($post_id, 'cptonomies_field_name', $value, false);
        }
      }
    }
    
  • You’re a rock star John! Thank you so much, that worked perfectly!

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

You must be logged in to reply to this topic.