Support » Themes and Templates » get unserialized custom post values

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

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get unserialized custom post values’ is closed to new replies.