Support

Account

Home Forums General Issues Guidance Required for Getting Values from a custom field using array.

Solving

Guidance Required for Getting Values from a custom field using array.

  • I’m struggling with returning the values from a custom field that is an array. Actually the data is storing by a plugin in a different way like There are number of groups and each group is storing number of attributes in a single custom field in shape of an array.

    Why i need this is just to create a front-end form so that my users can submit entries to this particular post type from the frontend.

    Please have a look at this image of database table, which is self-explanation of what i am trying to say.
    https://www.dropbox.com/s/z3uizu4i4nqb6de/database.png?dl=0

    The custom field can be a checkbox,text field,number etc.

    However, when I try to use the native WP functions the best I can do is to get a long string that looks like this:

    a:5:{s:4:"type";s:3:"Bar";s:10:"dimensions";s:51:"109.9 x 54 x 13.2 mm, 67 cc (4.33 x 2.13 x 0.52 in)";s:6:"weight";s:15:"102 g (3.60 oz)";s:10:"protection";s:0:"";s:6:"colors";s:12:"Black, White";}

    What I need is: Type , Bar, dimensions, etc..
    You can see this meta key is for group2 in the above image. I have 4 groups and all are storing values in array.

    I’ve spent days searching through various forums and tried a lot of suggestions but none worked – I either get just the word ‘array’, or the above string, or nothing at all.

    Ideally what I’d like is some simple code like this:

    $myvalues = get_post_meta($post->ID,'my_key');
    foreach ($myvalues as $myvalue) { echo $myvalue .', '; } endforeach;

    Is there a way to do what I need? I’d really rather not have to go thru and install the plugin for a bunch of sites where it’s not needed or have to maintain different themes for each.

    Thanks for any advice you can offer.

  • So for the broken image link. Here is the database table image.
    https://www.dropbox.com/s/z3uizu4i4nqb6de/database.png?dl=0

  • Not really an ACF question, but

    $myvalues = maybe_unserialize(get_post_meta($post->ID,'my_key'));

  • Thanks John for replying me. Actually, I am trying to setup frontend form using ACF plugin for a CPT that is also build using some other plugin but the values stored via that plugin is in arrray and difficult for me to return with a single meta key as ACF allows us while creating a form.

    The above code is not working. I am trying for achieving it from around a month with no success. or may be i am unable to explain the requirements in a sensible way 🙁

    Hope someday i will get over with this.

    Thanks again

  • You’re other plugin, whatever that is, is storing a serialized array. The code that I posted above is a WordPress function that will unserialize a value into an array if it is a serialized value.

    Beyond that I don’t think I quite understand what it is you’re trying to do.

    You’re trying to get this value stored by another plugin, but what are you trying to do with it? Are you trying to allow a front end ACF form to update that value so that the other plugin can find it and use the updated value?

  • Hi John, Sorry for late reply. Yes, I am trying to update that value which are stored by other plugin via ACF Front end form. So that the other plugin will find and use that value.

    The above code is something different. I am not understanding what to do with this code.

  • The code I posted above will get the value stored by the other plugin. In order to use ACF to store a value that the other plugin can use, you will need to create an acf/save_post action.

    http://www.advancedcustomfields.com/resources/acfsave_post/

    In this action you’ll need to get the values stored by ACF, convert them to an array and then use updte_post_meta to put the value into the database.

    http://codex.wordpress.org/Function_Reference/update_post_meta

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

The topic ‘Guidance Required for Getting Values from a custom field using array.’ is closed to new replies.