Post to category based on email domain, for a post-by-email plugin

I didn’t install the plugin to test this code.

You have to search the code to confirm it but the ’email_author’ seems to be the author email for this post. Why it is not named ‘author_email’ is anyone’s guess.

We should be able to add some code that checks for the incoming email:

if ( 'zzz.com' == $post['email_author'] ) {
    // Add category Z.
} else if ( 'yyy.com' == $post['email_author'] ) {
    // Add category Y.
}

But, first, let’s back up a bit. Your filter functions are all being called by the same hook (‘postie_post’). (A filter is a hook. The other type of WordPress hook is an action.) So you can place everything in the same function:

add_filter( 'postie_post', 'postie_custom_changes' );

/**
 * Make several custom changes to a Postie post.
 */
function postie_custom_changes( $post ) {

    if ( 'zzz.com' == $post->'email_author' ) {
        // Add category X.
    } else if ( 'zzz.com' == $post->'email_author' ) {
        // Add category Y.
    }

    // Prepend a link to bookmark the category of the post.
    $category = get_the_category( $post['ID'] );
    $link = '<a href="' . get_category_link( $category[0]->term_id ) . '">Bookmark this category</a>' . "\n";
    $post['post_content'] = $link . $post['post_content'];

    // Appends "(via postie)" to the title (subject).
    $post['post_title'] = $post['post_title'] . ' (via postie)';

    // Insert tags for a post.
    foreach ( $my_tags as array( 'cooking', 'latex', 'wordpress' ) ) {

        if ( stripos( $post['post_content'], $my_tag ) !== false )
            array_push( $post['tags_input'], $my_tag );
    }

    // Append "(via postie)" to the title (subject).
    add_post_meta( $post['ID'], 'postie', 'postie' );

    return $post;
}

The other changes I made were to the comments, the indentation and the I changed $this_cat to $category because abbreviating the word category bugs the heck out of me. 🙂

It looks like the ‘postie_post’ filter is run after the post has been created. A Google search of the Internet pipes and tubes turns up the WordPress function wp_set_post_categories()

The first parameter is a is the post ID and the second is an array containing the category IDs you want to set the post to. From your code it looks like you are expecting only one category per post, so let’s look at code to change the post category, not add it.

// Replace categories on some posts.
if ( 'zzz.com' == $post['email_author'] ) {
    wp_set_post_categories( $post['ID'], array( 2 ) );

} else if ( 'yyy.com' == $post['email_author'] ) {
    wp_set_post_categories( $post['ID'], array( 1 ) );
}

Assuming that category Y has an id of 1 and category Z has an id of 2.

You might also want to change the $post['category'] setting as well.

// Replace categories on some posts.
if ( 'zzz.com' == $post['email_author'] ) {
    wp_set_post_categories( $post['ID'], array( 2 ) );
    $post['category'] = array( 2 );

} else if ( 'yyy.com' == $post['email_author'] ) {
    wp_set_post_categories( $post['ID'], array( 1 ) );
    $post['category'] = array( 1 );
}