I was searching for a way to automatically insert a custom field when publishing a custom post type. And I repeatedly found the same solution, from here:
http://pippinspages.com/tutorials/publish-action-hook-for-custom-post-types/
I've tried this and for some reason it's just not working for me. I have a custom post type called sales_pages
Here's my code:
// function to be executed when a custom post type is published
function run_when_post_published($post_ID)
{
// your function code here
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, 'field-name', 'custom value', true);
}
}
// replace {custom_post_type_name} with the name of your post type
add_action('new_to_publish_sales_pages', 'run_when_post_published');
add_action('draft_to_publish_sales_pages', 'run_when_post_published');
add_action('pending_to_publish_sales_pages', 'run_when_post_published');
Can you please give me a hint as to what is wrong with this?
I've confirmed this working with regular posts (with the post publish hooks of course). It's just not doing the same for my custom post types.
Thanks in advance for any advice.
Cheers, Bryan