0

I want to know if is it required to have metaboxes set to save post meta into a custom post type.

I have a couple of pages where I save metadata into a post programatically, using add_post_meta, but I wanted to know if I can save data that is not set using add_meta_box.

Is it possible? What kind of considerations I should have about this?

4
  • 1
    You should always use update_post_meta() function because if the term doesn't exict it will make a call to the add_post_meta() automatically. The same goes for update_user_meta().Example if you want to ass the meta "City" but you don't have it in the db you simply make a update_post_meta($city) and in $city you need the post_id and the new value. For more information refer to : codex.wordpress.org/Function_Reference/update_post_meta – knif3r Jul 12 '16 at 17:58
  • Thanks for the heads up, @knif3r. Anyway, I wanted to know that, if no metaboxes are defined in my CPT, any meta data I save using update_user_meta() will be saved in the database or not. – noquierouser Jul 12 '16 at 19:40
  • 1
    Yes it will be saved :) – knif3r Jul 12 '16 at 20:40
  • Well, I did a couple of tests and you're right, the data is saved. Could you please write all this as an answer so I can mark it as the right answer? Thank you for your help, you're awesome. :D – noquierouser Jul 14 '16 at 14:51
1

You should always use update_post_meta() function because if the term doesn't exist it will make a call to the add_post_meta() automatically.

The same goes for update_user_meta().

Example if you want to add the meta "City" but you don't have it in the db you simply make a update_post_meta($city) and in $city you need the post_id and the new value for city. Like that :

$city = array(
'post_id' => $post_id,
'city' => 'Melbourne'
);

For more information refer to : codex.wordpress.org/Function_Reference/update_post_meta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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