1

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?

1 Answer 1

0

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 );
    }
}
1
  • 1
    thanks a lot! it seems I still need to look back to basic sometimes... it fixed by a simple statement.
    – niznet
    Commented Jan 2, 2020 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.