Question

I'm using transition_post_status to move a post into another post type based on the $new_status, I had made two hooks but the second one always firing first even though the $new_status is 'publish'. I has try to change the priority but didn't work either, try to combine them into one hook also not working either...

//Move Group Queue CPT to Group Post Type
add_action( 'transition_post_status', 'groupqueue_to_groupcpt', 10, 3);
function groupqueue_to_groupcpt($new_status, $old_status, $post){
    //Move to Group Post Type
    if ('publish' === $new_status
        && 'groupq' === $post->post_type
    )
        wp_update_post(array(
            'ID'        => $post->ID,
            'post_type' => 'group'
        ), true );
}

//Move Group Queue or Group CPT to Banned Post Type
add_action( 'transition_post_status', 'groupcpt_to_bannedcpt', 10, 5);
function groupcpt_to_bannedcpt($new_status, $old_status, $post){
    if ( 'banned' === $new_status
        && 'groupq' === $post->post_type xor 'group' === $post->post_type
    )
        wp_update_post(array(
            'ID'        => $post->ID,
            'post_type' => 'banned'
        ), true );
}

If I remove the groupcpt_to_bannedcpt, the first one will be running fine. Is there a solution to this matter?

Was it helpful?

Solution

You can user if..else condition to combine both function into one hook.

add_action( 'transition_post_status', 'groupqueue_to_groupcpt_and_groupcpt_to_bannedcpt', 10, 3);
function groupqueue_to_groupcpt_and_groupcpt_to_bannedcpt($new_status, $old_status, $post){
    //Move to Group Post Type
    if ('publish' === $new_status  && 'groupq' === $post->post_type )
    {    
        wp_update_post(array(
            'ID'        => $post->ID,
            'post_type' => 'group'
        ), true );
    }
    else if ( 'banned' === $new_status && ('groupq' === $post->post_type || 'group' === $post->post_type))
    {
        wp_update_post(array(
            'ID'        => $post->ID,
            'post_type' => 'banned'
        ), true );
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top