Skip to content
main logo
  • Blog
  • LinkedIn
  • GitHub
  • Recipes
Home » Blog » Create WordPress Post Meta When a Post is Created

Create WordPress Post Meta When a Post is Created

Posted by Ryan Neilson on January 20, 2023March 25, 2023 in PHP, WordPress

When working with WordPress post meta data, we may want to create our own custom post meta fields. To create WordPress post meta fields when a post is created, we can do so with a PHP function like this:

function set_custom_post_meta_on_insert_post( $post_id, $post, $update ) {

  // If this is a revision, is not of type 'post', or is an update, don't create the post meta
  if ( wp_is_post_revision( $post_id ) || 'post' !== $post->post_type || $update ) {
    return;
  }

  update_post_meta( $post_id, 'my_custom_post_meta', 'my initial value' );

}

We can then attach our function to the 'wp_insert_post' action hook:

add_action( 'wp_insert_post', 'set_custom_post_meta_on_insert_post', 10, 3 );

So our complete code looks like this:

function set_custom_post_meta_on_insert_post( $post_id, $post, $update ) {

  // If this is a revision, is not of type 'post', or is an update, don't create the post meta
  if ( wp_is_post_revision( $post_id ) || 'post' !== $post->post_type || $update ) {
    return;
  }

  update_post_meta( $post_id, 'my_custom_post_meta', 'my initial value' );

}

add_action( 'wp_insert_post', 'set_custom_post_meta_on_insert_post', 10, 3 );

A note on the above code: the function above assumes we only want to create a custom post meta on posts of type ‘post’. We can adjust this logic for any post type by modifying or extending the conditional check 'post' !== $post->post_type to include any post type we desire. For example, if we have a custom post type 'movie_review', we could change our function to:

function set_custom_post_meta_on_insert_post( $post_id, $post, $update ) {

  // If this is a revision, is not of type 'movie_review', or is an update, don't create the post meta
  if ( wp_is_post_revision( $post_id ) || 'movie_review' !== $post->post_type || $update ) {
    return;
  }

  update_post_meta( $post_id, 'my_custom_post_meta', 'my initial value' );

}

add_action( 'wp_insert_post', 'set_custom_post_meta_on_insert_post', 10, 3 );

More info on the WordPress hooks and functions used above:

  • update_post_meta
  • add_action
  • wp_insert_post

Share this article:
Tweet
Share
the author profile picture

Ryan Neilson is a software developer from Nova Scotia, Canada, where he lives with his wife and their menagerie of pets. He has worked extensively with PHP, WordPress, JavaScript, React, SCSS and CSS. Ryan can be found professionally at News Corp, where he works as a lead software engineer.

linkedingithub

Related Posts

Add WordPress User to Multisite via WP CLI

Adding a new user to a sub-site of a WordPress multisite is a two step process via the WP CLI. Add a user to the network First we’ll add a new user to the multisite network with the following WP CLI command: In this example we’ve assigned the role of administrator, while the other options …

Continue reading “Add WordPress User to Multisite via WP CLI”

Change Autocompleter Triggers in the WordPress Block Editor

We can use filters to change the autocompleter triggers in the WordPress block editor. By ‘triggers’, I mean the typed character combination we use to make an autocompleter appear. For example, by default we can use @ to populate a list of site users. We can also use [[ to select a post link from …

Continue reading “Change Autocompleter Triggers in the WordPress Block Editor”

© 2025 Neilson Web Design