Support » Plugins and Hacks » Hacks » Send emails to some mail address if category id is = get_the_category($post_id)

Send emails to some mail address if category id is = get_the_category($post_id)

  • 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);
    		$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); ?>
            $message = ob_get_contents();
            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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • get_the_category() returns the entire category object, not just an ID. To check the IDs in your conditionals you need to use something like: $postCategory->cat_ID == 11

    Tip: Instead of a string of OR’d comparisons, use in_array() to see if the cat ID is in an array of IDs:

    $match = array( 11, 59, 15, 22,);
    if ( in_array( $postCategory->cat_ID, $match )) //do stuff

    Thank you for your reply bcworkz.
    I got the idea and i made the changes but seems that it is not triggering the wp_mail. I tried to add an ELSE statement on it and then the wp_mail function triggers and sent the mail. This mean that is not finding/recognizing any category id. I’m missing something but i don’t know what 🙁
    Now, it looks like this:

    $matchEN = array( 40,60,51,44);
    		if ( in_array( $postCategory->cat_ID, $matchEN )) {
    				$sendTo = 'my@mail.com';
    				$email_subject = "[EN - ATA: ".$postCategory->cat_name ." ] ".$postTitle;

    Thank you!

    Argh! My bad. I need to read the function references more carefully 🙁
    My sincere apologies.

    get_the_category(), despite the singular form of the function name, returns an array of objects, not a singular object as I had assumed. If the posts only have one category assigned, use $postCategory[0]->cat_ID to reference the ID.

    If there could be multiple categories and you must trap a match of any of those categories to those in the array, you need to run a foreach loop to check each category, setting a flag if there is a match. Then only of the flag is set is the email sent. Something like this:

    $flag = false;
    foreach $postCategory as $cat {
       if ( in_array( $cat->cat_ID, $matchEN )) $flag = true;
    if ( $flag ) {
       //set mail parameters and send mail

    One more tip. I’m assuming it is possible that none of the conditions match, so no mail parameters are defined. Even if it’s not currently possible, it may be a good idea to assume it could be possible. Place the wp_mail() call inside the if() block that defines the parameters. Where it is now, it doesn’t send mail because the parameters are not defined, but the lack of definition is also generating PHP warnings. It all works, but it’s sloppy coding.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Send emails to some mail address if category id is = get_the_category($post_id)’ is closed to new replies.