My site has custom post type ‘Portfolio’. If a user has at least one Portfolio, how can I disable the option to delete that user?
I found the action/hook delete_user
, but it doesn’t seem right for this issue.
My site has custom post type ‘Portfolio’. If a user has at least one Portfolio, how can I disable the option to delete that user?
I found the action/hook delete_user
, but it doesn’t seem right for this issue.
When you click on “delete”, the action ‘delete_user’ will be launched: https://core.trac.wordpress.org/browser/tags/4.4.1/src/wp-admin/includes/user.php#L313
After that you can check, if the user has written at least one ‘portfolio’ post.
add_action('delete_user', 'sw_portfolio_check');
function sw_portfolio_check( $user_id ) {
$result = new WP_Query(
array(
'author'=>$user_id,
'post_type'=>'portfolio',
'posts_per_page'=>1,
)
);
if ( count($result->posts) !== 0 ){
wp_die("User has a portfolio and can't be deleted");
}
}