我不认为$post
将定义您在何处使用它。请尝试依赖传递给函数的post ID:
add_action(\'before_delete_post\', \'my_deleted_post\');
function my_deleted_post($post_id){
if ( "agency" == get_post_type( $post_id ) ) {
require_once(ABSPATH.\'wp-admin/includes/user.php\' );
wp_delete_user( intval( get_post_field( \'post_author\', $post_id ) ) );
}
}
请注意
wp_delete_user()
将尝试删除同一用户拥有的任何其他帖子,任何帖子类型,这可能会导致无限递归循环。使用第三个参数
wp_delete_user
重新分配其他职位可能是个好主意。或者在尝试删除用户之前,只需从挂钩中删除您的函数:
add_action(\'before_delete_post\', \'my_deleted_post\');
function my_deleted_post($post_id){
if ( "agency" == get_post_type( $post_id ) ) {
remove_action(\'before_delete_post\', \'my_deleted_post\');
// break unwanted recursion
require_once(ABSPATH.\'wp-admin/includes/user.php\' );
wp_delete_user( intval( get_post_field( \'post_author\', $post_id ) ) );
}
}