Calling a Function After New Post Creation for a WordPress Custom Post Type?

Assume I created a new post type 'product'. Is there a way to perform a function (user created) when this new 'product' post has been created?

Related posts

Leave a Reply

3 comments

  1. Hi @dotty:

    Take a look at the end of the wp_insert_post() function in the file /wp-includes/post.php (on lines 2148 thru 2392 in WordPress 3.0.1. Note that WordPress uses this function both for adding and updating posts.)

    At the end it has the following code. From this code you can identify the call to wp_transition_post_status() (more on that in a bit) and we have the action hooks edit_post, post_updated, save_post and wp_insert_post (frankly I don’t know why we have the latter two instead of just one.) You can use any of those that are appropriate for your needs:

    <?php
    wp_transition_post_status($data['post_status'], $previous_status, $post);
    if ( $update ) {
      do_action('edit_post', $post_ID, $post);
      $post_after = get_post($post_ID);
      do_action( 'post_updated', $post_ID, $post_after, $post_before);
    }
    do_action('save_post', $post_ID, $post);
    do_action('wp_insert_post', $post_ID, $post);
    return $post_ID;
    

    And as @Jan Fabry mentioned there are the action hooks found in wp_transition_post_status() (on 2713 thru 2717 of /wp-includes/post.php in WordPress 3.0.1.) Note there are three of them; use as appropriate:

    <?php
    function wp_transition_post_status($new_status, $old_status, $post) {
      do_action('transition_post_status', $new_status, $old_status, $post);
      do_action("${old_status}_to_$new_status", $post);
      do_action("${new_status}_$post->post_type", $post->ID, $post);
    }