Support

Account

Home Forums General Issues Want to use get_post_meta instead of get_field for array

Solved

Want to use get_post_meta instead of get_field for array

  • I’m struggling with returning the values from a custom field that is an array because I need to use get_post_meta instead of the ACF functions get_field/the_field. The reason is a long story but the bottom line is that the theme has to work on a multi-site installation for which ACF is not installed on all site/installs so using get_field/the_field returns an error where ACF is not in use.

    The custom field is a checkbox with multiple options that can be checked so that can be anything from 1 to 4 values for this field.

    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:4:{i:0;s:6:”value_1″;i:1;s:10:”value_2″;i:2;s:10:”value_3″;i:3;s:10:”value_4″;}

    What I need is: Value 1, Value 2, Value 3, Value 4

    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.

  • I think this is what you’ll want to do:

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

    The value a:4:{i:0;s:7:”value_1″;i:1;s:7:”value_2″;i:2;s:7:”value_3″;i:3;s:7:”value_4″;} that you returning is just a serialized or stringified version of the php array that you want to loop through.

  • Thanks – I understand what that snippet does and it makes sense that it should work, but I get this error message:

    Warning: unserialize() expects parameter 1 to be string, array given in (followed by the path to my theme file).

    I tried changing it to add true to indicate that it’s a string and not an array(?) but that didn’t change the error.

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

    Any ideas?

  • Try this:

    $grabMeta = get_post_meta( $post->ID, 'my_key', true );
    
    $myvalues = unserialize( $grabMeta );
    
    foreach ( $myvalues as $myvalue ) { 
        echo $myvalue .', '; 
    }
  • OK I got it worked out (whew!)…thanks to @ericnkatz who got me to dig deeper and read more about serialized strings as arrays

    SO what I had to do was setup the variable using ‘true’ to indicate that the meta data is, in fact, a string not an array (yes it’s multiple values for the key but it’s all stored as a single string since as mentioned in my original post it’s a series of checkboxes for one key)…..THEN run a foreach on the results. It didn’t work before because I was doing it bass-ackwards.

    The right way is this:

    $myvalues = get_post_meta($post->ID,'my_key',true); //using 'true' here is vital
    foreach ($myvalues as $myvalue) { echo $myvalue.', '; }

    Thanks!

  • Glad I could help, but I think you still have to unserialize the $myvalues prior to looping into the foreach.

  • Hi, could you please explain how u managed this problem, how should i use this code?

    I have a big form full of checkbox fields and after export in excel i get not separated with comma data, but something like this
    a:4{i:0;s:6:”value_1″;i:1;s:10:”value_2″;i:2;s:10:”value_3″;i:3;s:10:”value_4″;}

    I’d appreciate your help very much!

  • Hi Galtieri,

    First, are you talking about an actual form? Where data is submitted? If that’s the case, I would not suggest using ACF for this…instead I’d suggest using Gravity Forms or Formidable, both of which store form entries and have export functions that will give you exactly what you want, easily imported into Excel.

    Perhaps if you can explain more about how you’re using ACF and why you’re exporting to Excel, I can help you come up with a solution.

  • Hi TrishaM, thank you for respond.

    I build a base of children for russian medical institutions. It was planned so worker from institution can add child to database and add services that child got there. So theese are checkboxes for services https://yadi.sk/d/_6SEkMP3V3cQC.

    I tell it because key function for me is repeater field, not sure if GF of Formidable have it. Also i bought this plugin and set up whole website for it, so i don’t rly want to give up so easily.

    Close to the problem. In database it is saved serialized, like this https://yadi.sk/d/V4OiXtalV3d4o . I export posts and meta to Excel using SQL SELECT and i get this serialized data there too…

    And I can’t even say what i expect to see as a solution, all of them seem to be impracticable. It might be:
    1) Changing way of writing multiple data in ACF, so i get not serialized data.
    2) Enhance export to get unserialized data.
    3) Unserialize data in CSV file.

    Thank you for help.

  • @Galtieri – I apologize for the delayed response….

    My suggestion would be to not use ACF for the service category but instead use either Categories or Tags.

    You can create custom categories and custom tags if you’re using a custom post type for the children and don’t want to mingle either the categories or tags to indicate the services with categories or tags for other types of posts…..there is instructions in the WordPress Codex on how to do that.

    But if your Posts are just used for entering Children into a database, then I would suggest using ACF for all the other fields, and use the standard WP categories or Tags for the services.

    If that doesn’t work out for you, then I would suggest posting a new topic of your own, as this one I started has already been marked as “solved” since my original problem is solved.

    Good luck with your project!

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

The topic ‘Want to use get_post_meta instead of get_field for array’ is closed to new replies.