Adding Custom MetaData

by Joseph   Last Updated June 08, 2019 00:08 AM

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?

Answers 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 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.

June 07, 2019 23:34 PM

Related Questions