
Ever wanted to be able to automatically create a custom field with a value when a post (or page) is created? Then this snippet is for you.

Ever wanted to be able to automatically create a custom field with a value when a post (or page) is created? Then this snippet is for you.
Paste the code below into your functions.php file. The only thing you have to do is to edit the cutsom field name on line 6.
add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, 'field-name', 'custom value', true);
}
}
Thanks to wpCanyon for this cool tip!
24 Responses
Glad you like the snippet, Jean. And thanks for the mention
Also the custom field value should be changed (“custom value”) on the line 6.
In which scenario would it be useful?
@Paul: I recently worked on a project with a query_posts() based on custom fields. If a custom field hasn’t been created, the related post is not retrieved by the query.
@Paul – Had the same question on my blog about that. I worked on a project where the client wanted a simple custom popular posts plugin based on post views. This is what the plugin is using to create the custom field for the view counting + another little snippet to create the custom field on posts that don’t have it (posts published when the plugin wasn’t active).
Cool, thanks a lot! I was actually just looking for a code like this a few days ago and couldn’t find anything. I appreciate it =)
thanks for the explanation both of you.
You’re welcome Paul.
Haha…, thanks friend, am searching for similar post for 3 months…!
Very glad to see this hear…
Would it be possible to do this in the actual template after a check to see if the field is empty? I’ve got a series of custom fields I’m trying to tally up and add into it’s own field but having a hard time figuring it out… I’ve got the fields added up but I’d rather do it once instead of EVERY time the post(s) load.
So simple and so cool! Thanks!
@JD – Not sure i understood you correctly but if you mean that this way the custom fields gets added every time the post(s) load, you’re wrong. It’s adding the custom field only on post/page publish, which happens only one time. If i misunderstood you, please explain a bit further.
I’ve got 10 pre-defined custom fields that contain 1-10 “scores” that are added up when the post is displayed giving me a value like 98. Is it possible to use this to assign that total value to a new custom field I can then call on in the theme instead of calling data from 10 fields and adding them up. It would really only need to be done once since the scores wouldn’t change often if at all.
How can we get title of the current post before add_post_meta?
So, wouldn’t I be modifying my core WP installation? And if I upgrade it, the changes will be gone, no?
@mavtraveler No, you edit functions.php in your theme’s directory not a core wp file. /wp-content/themes/your_theme/functions.php
Am I able to somehow fill this newborn custom field? I’d like to fill it automatically with an author image url based on Author Image plugin’s function
And the function for previous message is the_author_image();
Can we create a custom field but then decide the value for that custom field in the post creation page within the wp-admin?
What if we want to remove the old custom values?
thanks for this nice tips
For users thats looking for more flexibility, you may consider the Supple Forms plugin. But I am really loving the tips from this site.
Thanks and I am definitely a fan now.
I think you’re the only place on the Web (well.. at least in Google’s opinion) that lists this tutorial!
Actually, most people probably won’t use it. But when you have to create 100 generic posts (using WP as a catalog/cart) my fingers are so grateful! I added this my my WP_INSERT_POST() function, and presto! No typing required. 100 posts, all categorized and everything, WITH custom fields data, up and running in no time.
Good show!
@Daniel – That’s strange, i should be there too. What was the search term?
Trackbacks: