0

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}
1

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($post->post_type == 'property') {
        // custom code here
    }
}

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

Also you don't need to check if post type is identical (===) to "property". It must be equal (==). See PHP Comparison Operators.

  • thanks @Max but its not working. however i have used new_to_publish but it still not working – tinku Feb 9 '16 at 10:01
  • Touche. I'll update my answer. – Max Yudin Feb 9 '16 at 11:17
1

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 ); 
  • Not wanting to be a smart*ss @Pieter, but does a (new)post which will be created not directly get a post_status of auto-draft? (and if, than it must be sure that transition_post_status this not ignores?) – Charles Feb 9 '16 at 12:23
  • 1
    Sorry for only coming back now, I was stuck with my mobile, and for some unknown crappy reason I cannot post comments from my mobile. Yes, it is true that posts get assigned as auto-draft, but we cannot go on this. What happens if we click safe draft and come back later to publish it. Our status is not auto-draft anymore, but only draft. Also, future posts are also a new post when it finally get published. Checking if the previous status is not published is a more reliable way to do this as this will take care of future, draft and auto-draft – Pieter Goosen Feb 9 '16 at 15:15
  • Thank you @Pieter for the explanation, I am still struggling with this and your response makes it a little brighter at the horizon. – Charles Feb 9 '16 at 15:51
  • So you want to say that I have to cycle through all (any) transitions. And than each of them to check Post Types? May be define('WP_POST_REVISIONS', false ); are set. If not, the transition will be false. – Max Yudin Feb 9 '16 at 17:37
  • 1
    @MaxYudin No, what I have posted is exactly what new_to_publish and publish_property does. My code is just more generic and much more flexible as it is not bound to one specific aspect. You can copy my code as-is and test it – Pieter Goosen Feb 9 '16 at 17:47
0

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

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.