• Resolved pax1

    (@pax1)


    Hi,

    I have a little issue sending mails after a post was published. It looks like WP is always putting my new post in the default category first and than in the selected one.

    Here is my setup and what I want to do:
    Different user roles should get notifications depending on the selected post category. I wanted to use “Buddypress” in combination with “Publish Press” but never got it to work, so I built a debugging function:

    function on_all_status_transitions($ID, $post ) {
        $author = $post->post_author; /* Post author ID. */
        $name = get_the_author_meta( 'display_name', $author );
        $email = 'mail';
        $title = $post->post_title;
        $permalink = get_permalink( $ID );
        $edit = get_edit_post_link( $ID, '' );
        $to[] = sprintf( '%s <%s>', $name, $email );
        $category = get_the_category($post->ID);
        $subject = sprintf( 'Published: %s', $title );
        $message .= $category[0]->name;
        $headers[] = '';
        wp_mail( $to, $subject, $message, $headers );
    }
    add_action(  'publish_post',  'on_all_status_transitions', 10, 2 );

    After publishing a new post, I always get 2 Mails. The first one for the default category and a few seconds later one mail with the right category. Because I want to use an if statement based on the category, it never works because I always get the default category sent to my function…

    Here’s a Screenshot

    Can anyone maybe help me with that?

    Thanks in advance,
    Pascal

Viewing 4 replies - 1 through 4 (of 4 total)
  • Luca Grandicelli

    (@lucagrandicelli)

    Hi @pax1,
    i guess youe $category is null..
    Please make sure the code is pulling out the correct values. Try to var_dump (or even better: debug) your code at $category and check if 1. Is an array 2. is not empty 3. has multiple values.
    It might turn out that you should call $category[1]->name.
    Let us know.

    Thread Starter pax1

    (@pax1)

    Hi Luca,

    thanks for the fast reply.
    I just checked it. My sizeOf($category) is 1. So I don’t have mulitple values.
    When I use $category[1] nothing shows up..

    Luca Grandicelli

    (@lucagrandicelli)

    Ok, consider that WP assigns the “undefined” category by default to new posts. What about vardumping that variable? Is “undefined” in it? Please also have a look at your database in wp_posts to make sure the post you’re trying to email has the right category stored.

    Thread Starter pax1

    (@pax1)

    So the post has the right categories (can’t get vardumping to work, I always get an error….) but you gave me an idea. I never want to post in the default category anyway so I do an if statement before sending my mails:

    if($category[0]->name != ‘Default’){
    wp_mail( $to, $subject, $message, $headers );
    }

    Not perfect but the only thing I can think of that works right now…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Set Post Category Not Working’ is closed to new replies.