admin管理员组

文章数量:1305525

Could you please let me know wordpress action hook that must triggered only once when a user create a new custom post.

That means if user creates a new post that hook must be triggered but when user update or edit the post then that hook should not be executed.

Previously i was using publish_post but it always executed when user create, edit or update the post however i have added some conditions to prevent this action but didn't works for me.

The code is as follow:

add_action( 'publish_property', 'pms_post_published_notification', 10, 2 );


    function pms_post_published_notification( $ID, $post ) {
         if(get_post_field('post_type',$ID ) === 'property' && !wp_is_post_revision( $ID )){// custom code here}

Could you please let me know wordpress action hook that must triggered only once when a user create a new custom post.

That means if user creates a new post that hook must be triggered but when user update or edit the post then that hook should not be executed.

Previously i was using publish_post but it always executed when user create, edit or update the post however i have added some conditions to prevent this action but didn't works for me.

The code is as follow:

add_action( 'publish_property', 'pms_post_published_notification', 10, 2 );


    function pms_post_published_notification( $ID, $post ) {
         if(get_post_field('post_type',$ID ) === 'property' && !wp_is_post_revision( $ID )){// custom code here}
Share Improve this question edited Feb 9, 2016 at 9:26 Sisir 7,8718 gold badges61 silver badges99 bronze badges asked Feb 9, 2016 at 9:15 tinkutinku 1191 silver badge4 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

You have to use new_to_publish action as described on Post Status Transitions Codex page (updated according @prog comment):

add_action( 'new_to_publish', 'pms_post_published_notification', 10, 1 );

function pms_post_published_notification( $post ) {
    if( 'property' === $post->post_type ) {
        // custom code here
    }
}

The only argument is passed to your function is $post object, so you can utilize it to check the post type.

  • Post Status Transitions
  • {$old_status}_to_{$new_status} action

You can always make use of the more global transition_post_status action hook which fires on every transition status for all post types.

Three values are passed by reference to the hook

  • $new_status which is the post status the post will have after transition

  • $old_status which is the post status the post had before transitioning

  • $post which is the current post object of the post being transitioned

Because a new post does not have any concrete set old post status, and definitely does not have an old status of publish, we can use that as logic to test against and then run our custom code based on that

add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
{
    if (    'publish' === $new_status
         && 'publish' !== $old_status
         && 'property' === $post->post_type
    ) {
        // This is a new property post, lets do some work
        // Run your custom code here
    }
}, 10, 3 ); 

However the below given code works for me.

add_action( 'new_to_publish', 'pms_post_published_notification', 10, 1 );

function pms_post_published_notification( $post ) {
    if($post->post_type == 'property') {
        // custom code here
    }
}

Thanks @Max

本文标签: Action hook on only publishing post not on editing or updating