0

I need to have 8 separate post meta fields on each post in wp-admin (shown in a custom meta field box).

I'd like the data from these 8 fields to be stored in an array as one post_meta field for easy parsing on the front-end.

What's the best way to do this? I have found examples of how to do this via PHP, but not how to combine inputs in the backend on save.

7
  • 1
    I would not recommend doing so. Mixing various fields in a serialized array makes a lot of other stuff more difficult later on, e.g. searching for them. Why don't you just build the array from separate values on the frontend?
    – kraftner
    Commented Jul 22, 2015 at 15:21
  • @kraftner - These values won't ever need to be searched. Basically I need to allow my editors to include 8 post ID's and custom titles for each of those ID's, so it makes more sense to have the ID's and titles grouped in an array.
    – jetlej
    Commented Jul 22, 2015 at 15:33
  • You could think of it as manually including related posts.
    – jetlej
    Commented Jul 22, 2015 at 15:34
  • Even more I believe this isn't the right approach. Your blocking a lot of unforeseeable possibilities you may need later on for no particular reason. What is the problem with building the array on the front-end?
    – kraftner
    Commented Jul 22, 2015 at 15:36
  • 1
    Check this answer if you are worried about performance ;-) Commented Jul 22, 2015 at 15:46

1 Answer 1

0

I agree with kraftner's comments to the question. This is a bad idea and you stand a good chance of regretting it later. Store you data as granular post meta values. Everything is easier later on.

However, add_post_meta() and save_post_meta() will serialize objects/arrays for you. You don't have to do anything special:

update_post_meta(1,'my_bad_idea',array('i','will','regret','this','later'));

And get_post_meta() will unserialize it for you:

var_dump(get_post_meta(1,'my_bad_idea'));
2
  • I know you can send an array of values to update_post_meta. My question was about how best to save the data from many input fields in wp-admin and save those as an array to post_meta.
    – jetlej
    Commented Jul 23, 2015 at 17:15
  • It is exactly the same answer -- build an array and save it. I don't understand what is so complicated.
    – s_ha_dum
    Commented Jul 23, 2015 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.