Post Pic

How to automatically create a custom field when a post is published

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

Jun 15 2010 12:16

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.

Jun 15 2010 12:54

In which scenario would it be useful?

Jun 15 2010 13:24

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

Jun 15 2010 13:36

@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).

Jun 15 2010 16:34

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 =)

Jun 16 2010 00:43

thanks for the explanation both of you.

Jun 16 2010 01:07

You’re welcome Paul.

Jun 16 2010 08:58

Haha…, thanks friend, am searching for similar post for 3 months…!

Very glad to see this hear… :D

Jun 17 2010 04:55

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.

Jun 17 2010 18:06

So simple and so cool! Thanks!

Jun 17 2010 23:52

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

Jun 18 2010 01:15

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.

Jun 19 2010 15:41

How can we get title of the current post before add_post_meta?

Jun 19 2010 22:22

So, wouldn’t I be modifying my core WP installation? And if I upgrade it, the changes will be gone, no?

Jun 20 2010 08:07

@mavtraveler No, you edit functions.php in your theme’s directory not a core wp file. /wp-content/themes/your_theme/functions.php

Jun 20 2010 14:10

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

Jun 20 2010 14:11

And the function for previous message is the_author_image();

Jun 21 2010 09:36

Can we create a custom field but then decide the value for that custom field in the post creation page within the wp-admin?

Jul 07 2010 10:45

What if we want to remove the old custom values?
thanks for this nice tips

Jul 13 2010 14:54

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.

Jul 26 2010 09:30

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!

Jul 26 2010 17:16

@Daniel – That’s strange, i should be there too. What was the search term?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required