8

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?

1
  • 5
    @dotty: Do any of these answers below help you solve your problem? If so, please up vote them to thank the people who've helped you, and pick the best answer so your question doesn't remain in the "unanswered" list. Sep 10, 2010 at 20:32

3 Answers 3

5

In functions.php:

function mynewproduct(){
    myfunction();
}
add_action( 'new_product', 'mynewproduct' );

For more and the official documentation, refer here:

http://codex.wordpress.org/Post_Status_Transitions

2
  • I don't think new_{post_type} will work because the status in the {status}_{post_type} action will never be set to new.
    – Spencer
    Jul 8, 2015 at 2:24
  • In this case it pays to know what's meant by 'created'
    – Tom J Nowell
    Jul 8, 2015 at 12:33
3

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

The wp_transition_post_status function will be called, and this will trigger the action "${new_status}_$post->post_type", so if a product moves to the publish state, the action publish_product will be triggered.

2
  • So i assign my function to the publish_product action?
    – dotty
    Sep 6, 2010 at 13:48
  • @dotty: You can do that, but if a user creates a product but does not yet click "Publish", this hook will not be triggered. This may or may not be what you want.
    – Jan Fabry
    Sep 6, 2010 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.