0

In Professional Wordpress Design & Development the author states that WordPress features a simple function to add new post metadata called add_post_meta().

This function will attach a piece of metadata to the post as follows:

   <?php  add_post_meta ( $post_id, $meta_key, $meta_value, $unique ); ?>

I've tried adding this code to my functions.php file without the < ? php ? > , and nothing happens. Where would this code go to successfully add a custom metadata box to a post?

1

that function is explained here and if I understand it right, it's not quite THAT simple. To use it as a single line in functions, you would have to fill out all those variables.

 add_post_meta ( 7, 'fruit', 'banana' )

This would literally output only on the admin of Post ID 7, and it would create a field called "fruit" with the value of "banana".

So it's simple...in the fact that it can be used in a more complex function to create post meta boxes. You would need to run this in a loop to get it to attach to every post, and depending on how robust you want your fields to be. I'm also not sure if it saves (it looks to me like you need to write that functionality in).

If you're looking to add custom meta boxes to your posts, I HIGHLY recommend using Advanced Custom Fields. You don't need Pro to use it, though it's 100% worth the purchase. It will easily assign to all posts of a type, or to individual posts and pages, it saves when edited, and it's got great documentation for how to call the data into your templates.

  • Using Advanced Custom Fields, the php functions are written for me. A breeze. The author was trying to teach us the manual way to do it, and just used that one simple function as a solution without mentioning the file that the function goes in, or like you said, how to run this in a loop.... – West Jun 8 at 2:55
  • I'm glad ACF got you sorted out! – Faye Jun 11 at 1:08

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.