Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add custom post type support to this function WordPress

  • SOLVED

So this function works for posts, but not for custom post types. How would you modify it to work with custom post types by the name of type1, type2 and type3? Or if it's easier, all existing post types?

function email_author_about_publishing() {
global $wpdb, $post_ID;

$users = $wpdb->get_results( "SELECT ID FROM $wpdb->usermeta WHERE meta_key = 'wp_user_level' && meta_value > 0" );
$post = $wpdb->get_row( "SELECT post_author, post_title FROM $wpdb->posts WHERE ID = " . $post_ID );
$authordata = get_userdata( $post->post_author );

$blogname = get_settings( "blogname" );
$subject = "Your post has been published!";

$text ="Ciao ". $authordata->user_nicename . ",";
$text = $text . "\n\nYour post " . $post->post_title." has been published.";
$text = $text . "\n\nYou can read it here: " . get_permalink( $post_ID );

mail( $authordata->user_email,
$subject,
$text,
"From: " . $blogname . " <" . get_settings( "admin_email" ) . ">" );


}

add_action('publish_post', 'email_author_about_publishing');


Thanks for your answers!

Answers (4)

2011-09-08

Pippin Williamson answers:

You have to use a couple more add_action hooks:


add_action('publish_post', 'email_author_about_publishing');
add_action('publish_custom_post_type_name', 'email_author_about_publishing');


Replace "custom_post_type_name" with the name of your post type.


Jeremy Phillips comments:

Thanks, that did it! Sorry John, looks like you where beat by a minute!

2011-09-08
2011-09-08

John Cotton answers:

You need to add more actions....

add_action('publish_type1', 'email_author_about_publishing');

2011-09-08

Maor Barazany answers:

It appears that the function should work for each post or type, since it fetches the post ID, and not limiting by post_type.
Maybe try to replace the line at the top -

global $wpdb, $post_ID;

with this -

global $wpdb, $post;
$post_ID = $post->ID;