我正在使用transition_post_status
根据$new_status
, 我做了两个钩子,但第二个总是先开火,尽管$new_status
是\'publish
\'. 我试图改变优先级,但也没有成功,尝试将它们组合成一个钩子也没有成功。。。
//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 );
}
如果我删除
groupcpt_to_bannedcpt
, 第一个运行良好。这件事有解决办法吗?
最合适的回答,由SO网友:Chetan Vaghela 整理而成
您可以使用if..else
将两个功能合并为一个挂钩的条件。
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 );
}
}