Title: Category_id switch
Last modified: September 3, 2017

---

# Category_id switch

 *  [patbell101](https://wordpress.org/support/users/patbell101/)
 * (@patbell101)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/category_id-switch/)
 * I am trying to mail using a php switch structure according to category ID but
   I can’t seem to properly extract the ID that I am comparing, I am looking at 
   the url /term.php?taxonomy=category&tag_ID=44 to get the actual ID I need but
   I always drop out of the bottom of the switch without evaluating true (herewith
   an abbreviated example). What am I doing wrong?
 *     ```
       	$categories = get_the_category($post_id);
   
       	if ( ! empty( $categories ) ) {
       		$category_id =( $categories[0]-> term_id );  
       	}
   
       	switch ($category_id) {
           case 44 : //"Announcements":
       	$mailgun_listaddress = "cle_news@mg.bell-computing.com";
               $subject = "An Announcement from CLE term8id=". $category_id;
       	wp_mail( 'cle_webmaster@mg.bell-computing.com', $subject, $message );
       	break;
           case 9 : //"News":
       	$mailgun_listaddress = "cle_news@mg.bell-computing.com";
               $subject = "News from CLE". $category_id;
       	wp_mail( 'cle_webmaster@mg.bell-computing.com', $subject, $message );
       	break;
           default:
       	$mailgun_listaddress = "cle_webmaster@mg.bell-computing.com";
       	$subject = 'An unhandled post has been posted - id:'. $category_id;
             	wp_mail( 'cle_webmaster@mg.bell-computing.com', $subject, $message );
       	}
       ```
   
    -  This topic was modified 8 years, 9 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
      Reason: put code in backticks; moved to fixing from developing
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcategory_id-switch%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [8 years, 9 months ago](https://wordpress.org/support/topic/category_id-switch/#post-9464661)
 * What happens if $categories is empty? Do a vardump or print_r on $categories 
   before you do the test. If it’s not emtpy, dump out $category_id right before
   you do the switch.
 *  Thread Starter [patbell101](https://wordpress.org/support/users/patbell101/)
 * (@patbell101)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/category_id-switch/#post-9464698)
 * Well… if its empty, I want it to do the default, which it always does – probably
   because it IS empty… return print_r($categories) just returns array() many times.
   OK so what am I doing wrong , how can I get the actual new post category ID?
 * I seem to get some info from custom posts (Nextgen) when I disable thies it seems
   to get stuck in a loop mailing indefinitely
 * The rest of the code is below.
 * function my_project_updated_send_email( $post_id ) {
 *  // If this is just a revision, don’t send the email.
    if ( wp_is_post_revision(
   $post_id ) ) return;
 *  $post_title = get_the_title( $post_id );
    $post_url = get_permalink( $post_id);
 *  if ( ! empty( $categories ) ) {
    $category_id =( $categories[0]-> term_ID );}
 * return print_r($categories);
 *  $categories = get_the_category($post_id);
    $message = “A post has been updated
   on your website:\n\n”; $message .= $post_title . “: ” . $post_url . $category_id;
 *  switch ($category_id) {
    case 44 : //”Announcements”: $mailgun_listaddress =“
   cle_news@mg.bell-computing.com”; $subject = “An Announcement from CLE”. $category_id;
   wp_mail( ‘cle_webmaster@mg.bell-computing.com’, $subject, $message ); break; 
   case 9 : //”News”: $mailgun_listaddress = “cle_news@mg.bell-computing.com”; $
   subject = “News from CLE”. $category_id; wp_mail( ‘cle_webmaster@mg.bell-computing.
   com’, $subject, $message ); break; default: $mailgun_listaddress = “cle_webmaster@mg.
   bell-computing.com”; $subject = ‘An unhandled post has been posted €’. $category_id;
   wp_mail( ‘cle_webmaster@mg.bell-computing.com’, $subject, $message ); }
 * }
    add_action( ‘save_post’, ‘my_project_updated_send_email’ );
    -  This reply was modified 8 years, 9 months ago by [patbell101](https://wordpress.org/support/users/patbell101/).
    -  This reply was modified 8 years, 9 months ago by [patbell101](https://wordpress.org/support/users/patbell101/).
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [8 years, 9 months ago](https://wordpress.org/support/topic/category_id-switch/#post-9464720)
 * If it’s empty, $category_id is defined, so yo have an error condition. This is
   just basic PHP debugging.
 *  Thread Starter [patbell101](https://wordpress.org/support/users/patbell101/)
 * (@patbell101)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/category_id-switch/#post-9464953)
 * Sorry my first venture into actions,hooks and debugging. So basic is what I am.
   Trying to step beyond just implementation and falling at the first hurdle it 
   seems.
 * I can’t get debug messages to appear at all now. Can’t see anything in the browser
   or the webconsole. Got the following in wp_config too.
    define( ‘WP_DEBUG’, true);
   define( ‘WP_DEBUG_LOG’, true ); define( ‘WP_DEBUG_DISPLAY’, true ); Nothing current
   in my debug.log or my server logs
 * Help! Surely the following fires on new and updated posts and MUST have the current
   $post_id? Not a lot to do wrong it seemed.
 * function my_project_updated_send_email( $post_id ) {
    $post_title = get_the_title(
   $post_id ); $post_url = get_permalink( $post_id ); $categories = get_the_category(
   $post_id); return print_r($post_title); return print_r($post_url); return print_r(
   $categories); } add_action( ‘save_post’, ‘my_project_updated_send_email’ );

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Category_id switch’ is closed to new replies.

## Tags

 * [use](https://wordpress.org/support/topic-tag/use/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [patbell101](https://wordpress.org/support/users/patbell101/)
 * Last activity: [8 years, 9 months ago](https://wordpress.org/support/topic/category_id-switch/#post-9464953)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
