您可能需要使用transition_post_status
改为挂钩,例如:
add_action( \'transition_post_status\',
function( $new_status, $old_status, $post )
{
if( \'cpt\' === $post->post_type
&& \'publish\' === $new_status
&& \'publish\' !== $old_status
)
{
// do stuff
}
}
, 10, 3 );
为了更好地控制帖子状态的变化,并轻松检查帖子类型。
要对所有自定义帖子类型执行它(如回答中所要求的),我们可以检查_built-in
post类型对象的属性:
add_action( \'transition_post_status\',
function( $new_status, $old_status, $post ) {
// get post type object
$post_type_object = get_post_type_object( $post->post_type );
// Check is the post type object is not built-in (that is, it is a custom post type)
// and check that the transition is from "some status" to "publish"
if( ! $post_type_object->_builtin
&& \'publish\' === $new_status
&& \'publish\' !== $old_status
) {
// do stuff
}
}
, 10, 3 );