Hello to everyone.
I was looking around for a way to send automatic notifications to some mail address if a new post is published in some categories. I have found something around and modified a little bit but it's not working as i need it to. This is what i have at the moment:
function notify_new_post($post_id) {
if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
$post = get_post($post_id);
$postTitle=get_the_title($post_id);
$postCategory=get_the_category($post_id);
$postCategoryName = get_category($postCategory);
$headers = 'From: My Website <my@website.com>' . "\r\n";
if(($postCategory==11) || ($postCategory==59) || ($postCategory==15) || ($postCategory==22) ){
$sendTo = 'receiver@email.com';
$email_subject = "[AL-ATA: ".$postCategory->cat_name."] ".$postTitle;
}
if(($postCategory==40) || ($postCategory==60) || ($postCategory==51) || ($postCategory==44) ){
$sendTo = 'receiver@email.com';
$email_subject = "[EN-ATA: ".$postCategory->cat_name."] ".$postTitle;
}
ob_start(); ?>
<?php echo $postTitle; ?> <br />
<?php echo get_the_content($post_id); ?>
<?php
$message = ob_get_contents();
ob_end_clean();
wp_mail( $sendTo, $email_subject, $message, $headers );
}
}
add_action( 'publish_post', 'notify_new_post' );
It seems that is not sending in any emails.
If i remove the if statement and let it only check if new post is published and doesn't mater the category/categories id, it send the email.
I should say that a single post may have more than one category and i need to check all of those categories if one of them meats my needs.
Hope that someone can help me with this.
Thanks in advance!