• When I ask for a post’s meta value, it comes as an array or a string.

    Can I store an array of values?

    For example, if I have an array $postIDs=array(21,47,81,32), can I store it with update_post_meta($post_id, 'postIDs', $postIDs)?

    Currently, I’m joining the array into a string and storing that.

    $postIDstring = implode(",", $postIDs);
    update_post_meta($post_id, 'postIDs', $postIDstring);

    Then I’m extracting it:$postIDstrings=update_post_meta($post_id, 'postIDs');

    $postIDs=explode(",",$postIDstrings[0]);

    How does it work?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Yeah, no need to do all of that. You can store an array directly as you first suggested. The save/update/get post meta process automatically serializes and unserializes arrays for you, you don’t need to do anything special.

    FYI, even if the process was not automatic, it’d be easier to use serialize() and unserialize() instead of imploding and exploding, especially for hierarchical arrays with complex structures.

    Thread Starter dgcov

    (@dgcov)

    Thanks!

    That was exactly what I wanted to know.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Storing arrays in post_meta’ is closed to new replies.