1

i need to Delete wp_terms -> name field when a user is delete .

my requirement is admin needs to assign posts to specific users, that is admin need to add a post to user1 ,but user2 should not see that.for this i created a custom post and add taxonomy for that.And the terms are users Usernames .so i need to list the usernames of the users as terms in the taxonomy.when a new user is registered his username should updated in the wp_terms table also,so i will get the usernames as terms. this is working well ,now i need to delete the term from taxonomy when the user is removed.

i stucked here .please suggest some solution for this

what i have done is

add_action( 'delete_user', 'yg_user_delete', 10, 1 );

function yg_user_delete( $user_id ) {

$user_info = get_userdata($user_id); $user_name = $user_info->user_login;



wp_delete_term( $user_name, 'user1', array() );

}

here 'user1' is my taxonomy

2 Answers 2

0

Replace this with yours:

add_action( 'delete_user', 'yg_user_delete', 10, 1 );

function yg_user_delete( $user_id ) {

    $user_info = get_userdata($user_id); 
    $user_name = $user_info->user_login;

    print_r($user_info);

    wp_delete_term( get_term_by('name', $user_name, 'user1')->term_id, 'user1', array() );

}
8
  • its not working
    – Adarsh
    Aug 1, 2018 at 5:28
  • whats the error? Aug 1, 2018 at 5:35
  • when i delete the user the term is not deleting
    – Adarsh
    Aug 1, 2018 at 5:36
  • when you are adding term, are slug and term name the same?? Aug 1, 2018 at 5:39
  • yes the code i have done for adding term when user created is
    – Adarsh
    Aug 1, 2018 at 5:44
1

If I understand well you have a custom_taxonomy and the terms slugs of it correspond to the usernames..in this case try

function yg_user_delete( $user_id ) {
  $user_info = get_userdata($user_id); 
  $user_name = $user_info->user_login;

  wp_delete_term(get_term_by('slug', $user_name)->term_id,'custom_taxonomy');
}
7
  • yes ,when a wp user is removed the term should also be removed from the taxonomy
    – Adarsh
    Aug 1, 2018 at 5:10
  • And what if get_term_by won’t find such term? Some error processing would be nice in there ;) Aug 1, 2018 at 5:26
  • it will not happen .because when a new user is added, at that time only the new term will create by using the username of user.so the error will not come
    – Adarsh
    Aug 1, 2018 at 5:34
  • but the code is not working
    – Adarsh
    Aug 1, 2018 at 5:34
  • What is the custom taxonomy you use? Aug 1, 2018 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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