Do New_to_publish hooks work for custom post types
-
25-04-2021 - |
سؤال
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...
المحلول
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.
لا تنتمي إلى wordpress.stackexchange