Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Add Custom Fields Automatically on Post Publish in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Add Custom Fields Automatically on Post Publish in WordPress

When creating our WPBeginner Gallery Site, we needed to generate custom branded short urls automatically for each site submitted to the gallery, and then store them as a custom field as soon as the post was published. Now while we will leave the auto-generating shortlinks for each post for our next topic, we will cover how to add custom fields automatically on post publish in WordPress. This can be very useful for developers who are looking to push WordPress to the next level.

First thing you need to do is open your theme’s functions.php file and paste the following code:

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

Then simply replace the field-name and custom value with your Custom Field Name, and the Value. This is a relatively simple trick, but it can be very effective when trying to use WordPress for other than blog purposes.

Source: WPCanyon

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. 李国安 says:

    This action hooks has been deprecated since Version 2.3.

  2. Sateesh Raghuwanshi says:

    I need to add this action for custom post type named ‘ad_system’

  3. Ilan Perez says:

    I have tried your code block above and nothing happens.

    The new fields aren’t added to to post.

  4. nayan says:

    I want to add category Id for the post in the post_meta table. How can be the function function add_custom_field_automatically($post_ID) be twicked to accomodate that?


  5. chris says:

    Instead of adding the custom field at the time of creating the post, how do I display a custom field by default on the admin page?

  6. puanthanh says:

    it’s not adding to custom post type

    • Editorial Staff says:

      If you notice, the code above doesn’t have anything related to the custom post types. It only adds to Post and Page “content type”. So you would have to specify the hook for your custom post type.

      • puanthanh says:

        Thanks for the reply. Can you help me out on this code.
        add_action(‘publish_page’, ‘add_custom_field_automatically’);
        add_action(‘publish_post’, ‘add_custom_field_automatically’);
        add_action( ‘save_post’, ‘add_custom_field_automatically’ );

        function add_custom_field_automatically($post_ID) {
        global $post;
        if(!wp_is_post_revision($post_ID)) {
        global $wpdb;
        $user_id = $wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $post_ID");
        $themename = $wpdb->get_var("SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = ‘themeperauthor’ AND user_id = $user_id");
        add_post_meta($post_ID, ‘themeperauthor’, $themename, true);
        When the user meta field is changed, I want to automatically delete the custom field value and update with the new one

  7. brian says:

    I’ve been working on adding some hidden custom fields on post publish/update (by preceding the field name with “_”) but for some reason I have to update the post twice before the wp_postmeta entries are written to the database.

    I’ve tried messing with the priority and experimenting with other action hooks (draft_to_publish, edit_post, etc) but it doesn’t seem to make a difference.

    Any ideas?

  8. Boba says:

    Thanks for including the source link :)

  9. Daniel Suarez says:

    Thanks Otto another great tip!

  10. Piet says:

    will this work too for custom post types?
    sth like add_action(‘publish_custom-post-typ-name’, ‘add_custom_field_automatically’);

    • Otto says:

      Yes, it will.

      One downside to this technique that people should be aware of is that if somebody edits a published post, this hook WILL get fired again on the edit. Therefore, you need to check for the meta before adding it, or to update it, or to do whatever makes the most sense for your use-case.

      If you only want to get your code fired off on the initial publish only, then you can use the transition_post_status hook. This hook works like this:

      function example($new, $old, $post) {
      // $new is the new post status ('publish')
      // $old is the old post status ('draft')
      // $post is the complete Post Object (so use $post->ID for the ID, etc)

      Then, in here you can do a check for something like this:

      if ($new == 'publish' && $old != 'publish')

      To have your code only used when the post status actually transitions to publish from whatever it was before. This hook is fired at the same time as the {$status}_{$post-type} hooks are, so the operation of them is basically the same.

  11. Vivek Parmar says:

    thanks for this handy tip. previously while using custom fields i have to work manually. now this will do it automatically. thanks for saving precious time of me

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.