To remove all custom post, you can use the fallowing PHP script in your uninstall.php file or function/method defined in your plugin. For example, let’s pretend we are uninstalling a book custom post type plugin:

$books = get_posts(['post_type' => 'book', 'numberposts' => -1]);
foreach ($books as $book) {
    wp_delete_post($book->ID, true); // 'true' stands for force delete (delete if privet draft and so on ...)
}

If you plan on removing also a custom post and meta values stored in database, you might want to use an SQL query instead. Notice. In the query below, I use SELECT instead of DELETE clause. Never delete anything from a database unless you investigated the output first. So if you’re happy with the data you get back, you might swap SELECT with DELETE.

global $wpdb;
$wpdb->query("SELECT * FROM wp_posts WHERE post_type = 'book'");
$wpdb->query("SELECT * FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts)");
$wpdb->query("SELECT * FROM wp_term_relationship WHERE object_id NOT IN (SELECT id FROM wp_posts)");

You can run the above SQL QUERY in plugin unistall.php file or in one of your function/methods registered with WP build in function register_uninstall_hook( string $file, callable $callback )

You might also go ahead and register uninstall static method, for example:

<?php

/**
 * Shortcode class. PHP version 5.6+
 * 
 * @category Plugin\MyPlugin
 * @package  MyPlugin
 * @author   Wiktor Liszkiewicz <w.liszkiewicz@gmail.com>
 * @license  all rights reserved
 * @link     none
 */

namespace Plugin\MyPlugin;

use Plugin\MyPlugin\CustomPluginOnActivate;
use Plugin\MyPlugin\CustomPluginOnDeactivate;
use Plugin\MyPlugin\CustomPluginOnUninstall;
use Plugin\MyPlugin\Admin\AdminSettingPage;

/**
 * This class provide a set of shortcodes for woocommerce products,
 * categories and tags related to post_type=product
 *
 * @category Plugin\MyPlugin
 * @package  MyPlugin
 * @author   Wiktor Liszkiewicz <w.liszkiewicz@gmail.com>
 * @license  all rights reserved
 * @link     tag in class commentphpcs
 */
class CustomPlugin
{

    public static $pluginName = PLUGIN_NAME;
    // private $postName = "book";

    /* 
    * @var Plugin\MyPlugin\Admin\AdminSettingPage $_adminSettingPage 
    */
    private $_adminSettingPage;
    
    /**
     * Undocumented function
     *
     * @return void
     */
    public function __construct()
    {
        CustomPluginOnActivate::registerActivationHook();
        CustomPluginOnDeactivate::registerDeactivationHook();
        CustomPluginOnUninstall::registerUninstallHook();

        /* @var Plugin\MyPlugin\Admin\AdminSettingPage $this->_adminSettingPage */
        $this->_adminSettingPage = new AdminSettingPage();
    }

    /**
     * This function is responsible for registring js and css scripts
     * 
     * @see $this->enqueueAdmin() and $this->enqueueFront() methods
     *
     * @return void
     */
    public function register()
    {
        add_action('admin_enqueue_scripts', [$this, 'enqueueAdmin']);
        add_action('wp_enqueue_scripts', [$this, 'enqueueFront']);
        // add_action('init', [$this, 'registerPostType']);

        $this->_adminSettingPage->register();
    }

    /**
     * Enque css and js for admin area.
     *
     * @return void
     */
    public function enqueueAdmin()
    {
        wp_enqueue_style('adminpluginstyles', plugins_url('/assets/admin/admin.css', PLUGIN_MAIN_FILE));
        wp_enqueue_style('adminpluginscripts', plugins_url('/assets/admin/admin.js', PLUGIN_MAIN_FILE));
    }

    /**
     * Enque css and js for fornt area.
     *
     * @return void
     */
    public function enqueueFront()
    {
        wp_enqueue_style('frontpluginstyles', plugins_url('/assets/styles.css', PLUGIN_MAIN_FILE));
        wp_enqueue_style('frontpluginscripts', plugins_url('/assets/script.js', PLUGIN_MAIN_FILE));
    }
}

Then here you would create another file:

<?php

/**
 * Shortcode class. PHP version 5.6+
 * 
 * @category Plugin\MyPlugin\Front
 * @package  MyPlugin
 * @author   Wiktor Liszkiewicz <w.liszkiewicz@gmail.com>
 * @license  all rights reserved
 * @link     none
 */

namespace Plugin\MyPlugin;

if (! defined('ABSPATH')) {
    die('No direct access allowed');
}

/**
 * CustomPluginOnUninstall class is responsible to remove all data and 
 * files from WP system when user decide to delete the extension.
 * 
 * Beofre using this functionality make sure to create database backup!
 *
 * @category Plugin\MyPlugin
 * @package  MyPlugin
 * @author   Wiktor Liszkiewicz <w.liszkiewicz@gmail.com>
 * @license  all rights reserved
 * @link     none
 */
class CustomPluginOnUninstall
{

    /**
     * Static method - Select and remove all data from 
     * custom post registeded by this plugin.
     *
     * @return void
     */
    public static function uninstall()
    {
        global $wpdb;

        /**
         * TEST OUTPUT BEFORE CHANGING TO "DELETE"
         * 
         * This query removes all asosiations in post_meat to 
         * non exsisting posts in wp_posts table.
         */
        $wpdb->query("SELECT * FROM wp_posts WHERE post_type = 'book'");
        $wpdb->query("SELECT * FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts)");
        $wpdb->query("SELECT * FROM wp_term_relationship WHERE object_id NOT IN (SELECT id FROM wp_posts)");

        /* Uncoment only if SELECT output was tested (TEST FIRST!) */

        // $wpdb->query("DELETE FROM wp_posts WHERE post_type = 'book'");
        // $wpdb->query("SELECT FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts)");
        // $wpdb->query("SELECT FROM wp_term_relationship WHERE object_id NOT IN (SELECT id FROM wp_posts)");

        /* ALTERNATIVLY USE */

        // $books = get_posts(['post_type' => 'book', 'numberposts' => -1]);
        // foreach ($books as $book) {
        //     wp_delete_post($book->ID, true); // 'true' stands for force delete (delete if privet draft and so on ...)
        //     // add delete_post_meta() // you need to know the post meta keys here so you might need to query them first with SQL
        // }
        
    }

    /**
     * Static function which will call the unistall method of this class
     * Note the use of __CLASS__ instead of this as this is a static method.
     * 
     * @see called in CustomPlugin class in __construct method
     *
     * @return void
     */
    public static function registerUninstallHook()
    {
        register_uninstall_hook(PLUGIN_MAIN_FILE, [ __CLASS__, 'uninstall' ]);
    }

}

https://hotexamples.com/examples/-/-/delete_post_meta/php-delete_post_meta-function-examples.html
https://www.youtube.com/watch?v=x3ID1cyafvE

0
Would love your thoughts, please comment.x
()
x