Save an array of values in the post meta box

I created box meta for a custom post type. From the admin edit for the post, I want to enter in multiple values so that it saves as an array so I can then call it as an array from the front end.

So in my meta box from the admin edit I'd save the values "200", "201", "202" in a single field "_parent_id" and then grab it from the front end

$array = get_post_meta($postID, "_parent_id", false);

How should those values look in the box meta field?

Topic post-meta Wordpress

Category Web


Just save the array to post meta as you save string-

$data = array("200", "201", "202");

//Update inserts a new entry if it doesn't exist, updates otherwise
update_post_meta($post_ID, '_parent_id', $data);

The for getting the post call get_post_meta like below-

get_post_meta($post_ID, '_parent_id', true);

You'll get the array. Then run for or foreach loop to show the array data as you want to show.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.