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

Deleting Custom Posts & Meta Data on Uninstall

I just came across this in a search and thought I’d reply just in case you didn’t figure this out.

I’m not 100% sure the best way to go about this but I can provide some information that may be helpful.

$wpdb has both prefix and base_prefix members, so you can get the ‘wp_4_’ by referencing $wpdb->prefix and making use of that in your query.

Given what you have you can do a string replace on $postmeta_table and $posts_table so that if they are set to wp_ instead of your prefix (in this case ‘wp_4_’) then replace it and run your query.

It would be something like:

global $wpdb; // Must have this or else!

$postmeta_table = $wpdb->postmeta;
$posts_table = $wpdb->posts;

$postmeta_table = str_replace($wpdb->base_prefix, $wpdb->prefix, $postmeta_table);
$postmeta_table = str_replace($wpdb->base_prefix, $wpdb->prefix, $postmeta_table);

$wpdb->query("DELETE FROM " . $postmeta_table . " WHERE meta_key = '_mrlpt_client_email'");
$wpdb->query("DELETE FROM " . $postmeta_table . " WHERE meta_key = '_mrlpt_client_phone_num'");
$wpdb->query("DELETE FROM " . $posts_table . " WHERE post_type="mrlpt_client"");    

Again, I’m not sure this is the best way to do it but it should get you to where you are trying to go.

References:

  • PHP str_replace
  • WordPress Codex for $wpdb
  • WordPress Support – Multisite Table Prefix “$wpdb->prefix” incorrect

Related Posts:

  1. Advanced search form with filters for custom taxonomies and custom fields
  2. Meta query with boolean true/false value
  3. Get post with multiple meta keys and value
  4. Custom field values deleted when trashing custom post type
  5. How to get all custom fields of any post type
  6. Update CPT meta data using REST API
  7. How do I remove all the metaboxes for a custom post type?
  8. Custom Queries: Joining On Meta Values From Two Custom Post Types
  9. Gutenberg how to make attribute to save to meta
  10. Get all posts from custom post type and insert in select input as metabox
  11. Custom Post Type: Set post_title equal to a custom post type field
  12. I can’t set meta_key in my custom post type query
  13. WordPress Admin Panel search posts with custom post meta values along with title
  14. How to Update post status using meta data in Custom post TYpe
  15. Rest Api v2 orderby meta_key in custom post type
  16. bulk Update post_meta with default values
  17. How can I include meta box content when searching?
  18. Add Post Url to Manage Edit Screen
  19. Display all custom post type posts and order them by an optional meta_key
  20. How can I get_post_meta() in the most efficient way?
  21. need advice on how to do a lists using custom post types – taxonomy vs postmeta
  22. how to interconnect custom post types?
  23. Custom Post Status Transition Issues With Get Post Meta
  24. List all images from a single post meta value
  25. Show the same Article Available in Other Categories
  26. how to delete all users and posts based on ‘user_meta’?
  27. Meta Key Value in current-user-only loop
  28. How to set YouTube video as featured image?
  29. Custom Post Types. Are there any disadvantages/advantages in using a plugin to develop them?
  30. How to implement a Google map store locator
  31. Widget: Custom Post Type Post Listing Dropdown on Admin Side
  32. How do I create an archive page as a including metadata?
  33. How to duplicate entire custom post type
  34. Two Custom Post Types Many to Many Relationship
  35. get_post_meta not working on publishing
  36. how can I register a post_meta field in an existing CPT and then call it again with get_post_custom()?
  37. Update postmeta Parent when post_status child change
  38. WordPress request fiter order by related post’s post_title
  39. Update database from Quick Edit according to Checkbox state of Custom Post Type
  40. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  41. Dynamic Custom Fields
  42. Show Custom Post Type meta boxes only on Page Edit
  43. Getting thumbnails of a custom posts featured images in a page-templates metabox
  44. Remove Post Custom Meta Box
  45. Using preg_replace to filter custom textarea
  46. Check if post with same meta value exists
  47. If Post Meta Equals Current Title Conditional Advanced Custom Fields
  48. Do posts, pages and / or custom post type objects have unique ID numbers or can there be multiple objects with the same IDs?
  49. Archieve.php not loading for custom post type
  50. Meta data (Tags and Categories) for Custom Posts not showing.
  51. Display different information of a custom post type
  52. delete_post_meta() for whole CPT / multiple posts?
  53. How to get specific post meta by title or id
  54. How can I display a drop-down select of Post Names
  55. how to show records that don’t have custom meta value
  56. $post->post_meta not pulling any post in wordpress/php
  57. WP_Query of custom post type sorted by meta_key has unexpected results
  58. Cannot obtain custom meta information
  59. Get all metadata for a given custom post type
  60. Advanced custom field – posted fields from custom post type
  61. delete duplicate meta_value with same post_id
  62. Search CPT Title AND Meta
  63. Add a class to post_class if more than one post shares same meta_value_num
  64. Function to return values from metabox
  65. Add a meta field to the list of results for a custom post type
  66. Empty meta-box returns publishdate if no value is set?
  67. Filter search posts by post meta?
  68. Custom fields for post or terms which don’t update on post update
  69. Using publish_{custom-post-type} hook for custom post type to update meta doesn’t work
  70. Add a form in every post and save data in post meta
  71. How to use multiple Meta Field from CPT as Post permalink
  72. Unable to gather Image URL from Custom Post Type’s; Custom Meta Field
  73. Attaching more than 1 parent (Post) to a media file
  74. Use WP_query to match post types based on custom field values
  75. How can I add a meta[] to my custom post type and search by term with the Rest API?
  76. Creating an archive page or simple template to list all values of a custom field of specific post type listing
  77. Custom meta fields not showing up in WP_Response Object via custom endpoint
  78. How to get post by meta value
  79. Get meta values from parent post and save in child post
  80. How do I get all authors posts of a custom post type outside loop
  81. How to Disable option of meta field if that option is selected for any other post in custom post type?
  82. How to get post detail based on URL of the post
  83. Stored meta from attachment, video length?
  84. Custom post type meta fields missing on save_post_type
  85. Linking posts together with Advanced Custom Fields “both ways”
  86. Order posts by meta value hiding posts instead of re-ordering
  87. custom search results – order results differently by post type
  88. How to get Current week and current date record wp query
  89. get_post_meta for Custom Post Type ( CPT )
  90. Complex strcuture as CPT or taxonomy for use in woocommerce product variations [closed]
  91. How to add post reference field to a plugin?
  92. Custom Post Type meta oembed html output resulting in WSoD
  93. Create action running on trashed_post hook to modify post_meta value
  94. Random meta field from specific custom post type
  95. Custom Form Data save after Login User
  96. Changing to different post type maintains the redirection?
  97. Post Meta for Custom Post Types
  98. How do I add the “orange counter” next to a Custom Post Type?
  99. CPT meta values intermittently return empty
  100. Quering a post object based on another related post object
Categories custom-post-types Tags custom-post-types, post-meta, uninstallation
How To Remove The “Delete” Theme Option In Dashboard
Illegal characters in URLs

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