Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Do these hooks listed on the following site work for custom post types?

http://www.wphub.com/custom-post-type-publish-action-hook/

    // function to be executed when a custom post type is published
    function run_when_post_published()
    {
        // your function code here
    }

    // replace {custom_post_type_name} with the name of your post type
    add_action('new_to_publish_{custom_post_type_name}', 'run_when_post_published');        
    add_action('draft_to_publish_{custom_post_type_name}', 'run_when_post_published');      
    add_action('pending_to_publish_{custom_post_type_name}', 'run_when_post_published');

I cannot get them to work...

share|improve this question
    
What exactly have you tried so far? Show some code, please. –  kaiser Sep 3 '12 at 15:11
    
Please formulate your question in a meaningful and complete way, rather than sending people elsewhere to try and figure out what you mean. –  Rarst Sep 3 '12 at 15:30
    
updated question, that's the code I'm trying out. –  drtanz Sep 4 '12 at 9:28

1 Answer 1

up vote 1 down vote accepted

I think you're using wrong hooks. Please take a look at wp_transition_post_status function, you'll see the correct hooks:

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

So, in your code, you should use hooks:

'new_to_publish'   
'draft_to_publish'
'pending_to_publish'

and in your function, you have to check your post type.

share|improve this answer
    
How do I check my post type correctly? –  drtanz Sep 4 '12 at 11:21
    
I had changed my code in the meantime to use: add_action('publish_myt_cpt', 'myfunction'); The issue I have now is that myfunction() needs the ID of the latest inserted post, and it isn't finding it. So I am thinking that the hook is running before the post is published, making my function not produce the desired result. Is this so? –  drtanz Sep 4 '12 at 11:22
    
I also don't understand why to use the hooks you mentioned then check the post type within the function, when I can directly use an action which would fire only for the desired post type, making that check within the function redundant. –  drtanz Sep 4 '12 at 11:25
    
The other thing is that my function needs to be called from various places, even from cron for example, so that check for post type within the function would mess up things for me. It would be much better to have a hook that fires specifically when a defined custom post type is published. –  drtanz Sep 4 '12 at 11:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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