WordPress.org

Ready to get started?Download WordPress

Forums

get unserialized custom post values (3 posts)

  1. James Revillini
    Member
    Posted 3 years ago #

    I have a custom post type 'dogs.' I set up custom fields specifically for the dogs post type, and all of it is working great as far as getting the data in, but getting it out into the template is somewhat confusing due to some fields having serialized values.

    Examples:
    'Location' is a simple custom field which allows the user to enter the location of the dog into a text box. So when I do
    $location = get_post_custom_values('location');
    I can output that easily with
    echo $location[0];
    But if you look at 'sex', which is a radio button with values 'male' or 'female', it gets a little nutty
    $sex = get_post_custom_values('sex');
    echo $sex[0]; //outputs a serialized string
    $sex = unserialize($sex[0]);
    echo current($sex); //outputs the value I want

    Whenever the code gets silly like this, I can't help but think there is a better way, but I've searched the codex and only see auto-unserialization thru functions like get_post_meta, which don't work for the custom fields as far as I can tell.

  2. eritt
    Member
    Posted 2 years ago #

    thanks, you helped me a lot actually with your post - could not find this information anywhere!

  3. James Revillini
    Member
    Posted 2 years ago #

    Cool ... glad someone could use this info. :)

Topic Closed

This topic has been closed to new replies.

About this Topic