Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

how to delete all users and posts based on ‘user_meta’?

Because “institute” is a user and you want to perform some actions when it is deleted, you could use delete_user action (as we need information of the deleted user, deleted_user may not work, not sure about this but delete_user should work for sure). In that hook you can get all users and posts based on inistitute_name meta field and delete them.

Seeing your code, “Institute” user type can be identified by user role (you assing admin) and user meta inistitute_name. So, in delete_user action you can check if the deleted user is and admin and if it has institute_name assigned, in that case, the deleted user is a “institute” and you can get all other associated users and posts.

add_action( 'delete_user', 'cyb_delete_institute_info' );
function cyb_delete_institute_info( $id ) {

    $user = get_userdata( $id );

    if( ! empty( $user ) && in_array( "admin", (array) $user->roles ) ) {

        // Deleted user is admin, check `inistitute_name` meta field
        $institute_name = get_user_meta( $id, 'inistitute_name', true );

        if( ! empty( $institute_name ) ) {
            // Deleted user is an admin and has `institute_name` meta,
            // so, it is a "Institute" user

            // Get posts to delete
            $posts_args = array(
                'post_type' => array( 'courses', 'notifications' )
                'meta_query' => array(
                    'key'   => 'inistitute_name',
                    'value' => $institute_name
                ),
            );

            $query = new WP_Query( $posts_args );
            $posts_to_delete = $query->get_posts();

            foreach( $posts_to_delete as $post ) {
                wp_delete_post( $post->ID );
            }


            // Get users to delete
            $users_args = array(
                'role__in'       => array( 'trainer', 'trainee' ),
                'meta_query' => array(
                    'key'   => 'inistitute_name',
                    'value' => $institute_name
                ),
            );
            $query = new WP_User_Query( $users_args );
            $users_to_delete = $query->get_results();

            foreach( $users_to_delete as $user ) {
                wp_delete_user( $user->ID );
            }

        }

    }
}

Note: not tested, just written here as sample code.

Related Posts:

  1. Creating a custom post type upon registration
  2. Want to Add Custom Fields for Uploading video to WordPress Users from front end
  3. Restrict Access to Posts based on Custom User and Post Meta Data
  4. Users create/join groups
  5. Query posts based on the meta key values of logged-in users?
  6. Meta box with front-end styling
  7. Creating a custom post type upon registration for a specific user role
  8. Autofill advanced custom field with user data
  9. UPDATED: Save a custom_field value when automatically creating a post using wp_insert_post
  10. Creating multiple profiles under one user to assign to particular posts
  11. Display a post count from a custom metabox selection
  12. Display and Allow users to edit their own profiles
  13. How to display Author Profile based on Custom field value
  14. post meta parameter in post custom-post-type endpoint with restapi
  15. How do I set all of a particular post meta to a value within the custom post type I’m in?
  16. Update postmeta Parent when post_status child change
  17. Metabox not show in categories custom post type cmb2
  18. WordPress request fiter order by related post’s post_title
  19. Disable user from updating certain posts
  20. Update database from Quick Edit according to Checkbox state of Custom Post Type
  21. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  22. Storing a many to many post type relationship in post meta and keeping SQL ability for Joins
  23. Dynamic Custom Fields
  24. Show Custom Post Type meta boxes only on Page Edit
  25. How to restrict author to only access one custom post type ?
  26. Create custom post with custom user rules
  27. Custom filter function not working with Custom post type
  28. Associate multiple users to custom post type
  29. How to assign permissions for a CPT to a user
  30. Importing Data from a Non-WordPress database, into WP
  31. Getting thumbnails of a custom posts featured images in a page-templates metabox
  32. List Taxonomies: Don’t list taxonomy if it has no post – depending on custom post-meta?
  33. Remove Post Custom Meta Box
  34. Can’t publish custom post type – “You are not allowed to edit this post.”
  35. Custom fields for custom post type
  36. Show future events based on custom field AND order by date
  37. Meta Data for Custom Post Type not saving
  38. Using preg_replace to filter custom textarea
  39. Check if post with same meta value exists
  40. Filter date from post meta when date is in string format
  41. Get only used meta_values
  42. How can i display movies in profile page, added by an user?
  43. If Post Meta Equals Current Title Conditional Advanced Custom Fields
  44. Is there a way to exclude posts based on meta_values?
  45. cannot get user_registered date from get_user_meta
  46. Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?
  47. Archieve.php not loading for custom post type
  48. Submitting Custom Post Types with custom fields from Front-end form
  49. create custom meta box with default value
  50. Custom User role not working with custom post type
  51. Get $post->ID from a URL
  52. Displaying a div from an assigned meta_value when on a page
  53. Meta data (Tags and Categories) for Custom Posts not showing.
  54. Display different information of a custom post type
  55. Create Array from custom post type to display a slider
  56. Setting Post Title via Meta Data returns Auto-draft
  57. How to Echo Metadata Value in Currency Format
  58. Issue on Working with Metabox – Checkbox to enable Sale Items
  59. Filter posts by tax (dropdown) and meta value
  60. delete_post_meta() for whole CPT / multiple posts?
  61. get comments by current user inside page template
  62. How to get specific post meta by title or id
  63. Publish and save specific postmeta to a filtered post_type
  64. how to associate several authors to a custom post type
  65. How to move a post to different post type with all meta data?
  66. Assign post type to many users
  67. Why do I have to press the “Update” button twice to save my meta box values?
  68. How can I display a drop-down select of Post Names
  69. Check if user has comment on current post
  70. how to show records that don’t have custom meta value
  71. Query within a foreach within a query (queryception)
  72. WP API Response does not show my registered metadata
  73. How to detect that the save_post hook is calling the callback associated to the current edit post page only
  74. $post->post_meta not pulling any post in wordpress/php
  75. Values from meta-box not on $_POST
  76. WP_Query of custom post type sorted by meta_key has unexpected results
  77. Showing User’s Post Counts by Custom Post Type in the Author.php?
  78. Returning a custom content types with meta values
  79. Custom post type suddenly stoped working after WordPress 5 update
  80. Batch Extract Date from post title and put into ACF custom field
  81. copy images from custom field to another custom field
  82. Get current user id
  83. How to show custom field on specific custom post type posts only when filled
  84. How to handle this specific case of custom post type?
  85. Save CTP post meta different values each day without overwriting previous values
  86. get_post_meta returns NULL in front-end, but correct result in back-end
  87. Cannot obtain custom meta information
  88. Problem with adding custom post type meta to header by plugin
  89. is_main_query() never called on WP 4.4
  90. How can I output WPAlchemy repeating fields meta values in my page template?
  91. Show number of posts by logged in user
  92. Meta box data is saved but NOT displayed in the meta box text field. Why?
  93. How to call a post’s metadata in shortcode.php?
  94. Save Metabox Custom Field Value
  95. Echo custom post meta from options array
  96. Value of post meta dropdown is not showing in WordPress
  97. Meta box data not saving
  98. Get all metadata for a given custom post type
  99. WP_Meta_Query object with conditionals
  100. Trying to write shortcode with get_post_meta but isn’t working
Categories custom-post-types Tags custom-post-types, post-meta, user-meta, users
problem with WordPress ajax
New to wordpress: would like to have pluggable main page sections [closed]

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress