WordPress Content Cleanup Functions

Cleaning Up WordPress Content with Custom PHP Functions

When working with WordPress, you may encounter situations where you need to perform bulk content cleanup or remove specific shortcodes. Here are two powerful functions that can help with these tasks.

Important: Always back up your database before running these functions as they make irreversible changes.

1. Force Delete All Posts of a Specific Type

This function completely removes all posts of a specified type from your WordPress database, including their metadata and taxonomy relationships.

function force_delete_all_posts($post_type = 'post') {

    global $wpdb;

    // Get all post IDs for the given post type

    $post_ids = $wpdb->get_col( $wpdb->prepare(

        "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s",

        $post_type

    ));

    if ( empty($post_ids) ) return;

    $post_ids_in = implode(',', array_map('intval', $post_ids));

    // Delete from posts table

    $wpdb->query("DELETE FROM {$wpdb->posts} WHERE ID IN ($post_ids_in)");

    // Delete from postmeta table

    $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id IN ($post_ids_in)");

    // Delete from term relationships

    $wpdb->query("DELETE FROM {$wpdb->term_relationships} WHERE object_id IN ($post_ids_in)");

}

force_delete_all_posts();

When to Use This Function:

  • When migrating from a staging to production environment and needing to clear test content
  • When changing post types and needing to completely remove old entries
  • When performing a complete content reset for a client site

2. Cleaning Fusion Builder Shortcodes

This function helps remove all Fusion Builder shortcodes from your content, either dynamically when content is displayed or by updating all posts in the database.

Option 1: Dynamic Filter

function clean_fusion_shortcodes($content) {

    // Remove all [fusion_xxx] shortcodes including [fusion_global id="1654"]

    $content = preg_replace('/\[fusion_[^\]]*\]/', '', $content);

    // Remove all closing shortcodes [/fusion_xxx]

    $content = preg_replace('/\[\/fusion_[^\]]*\]/', '', $content);

    // Remove any [fusion_builder_container], [fusion_builder_row], etc

    $content = preg_replace('/\[(\/?fusion_[^\]]+)\]/', '', $content);

    // Optional: normalize white space

    $content = preg_replace('/\s+/', ' ', $content);

    return trim($content);

}

// Apply it dynamically to all post content

add_filter('the_content', 'clean_fusion_shortcodes', 20);

Option 2: Database Update

add_action('init', function() {

    $args = array(

        'post_type' => 'post',

        'posts_per_page' => -1,

        'post_status' => 'any'

    );

    $query = new WP_Query($args);

    foreach ($query->posts as $post) {

        $content = $post->post_content;

        $content = preg_replace('/\[fusion_[^\]]*\]/', '', $content);

        $content = preg_replace('/\[\/fusion_[^\]]*\]/', '', $content);

        $content = preg_replace('/\[(\/?fusion_[^\]]+)\]/', '', $content);

        // Update the post only if the content has changed

        if ($content !== $post->post_content) {

            wp_update_post([

                'ID' => $post->ID,

                'post_content' => $content

            ]);

        }

    }

});

When to Use These Functions:

  • When switching away from Avada's Fusion Builder and needing to clean up old shortcodes
  • When improving site performance by removing unused shortcode markup
  • When preparing content for migration to a different theme or page builder

Implementation Notes

These functions should be added to your theme's functions.php file or in a custom plugin. For the deletion function, you may want to:

  1. Create a temporary admin page with a button to trigger it
  2. Add confirmation checks before execution
  3. Implement error logging

For the shortcode cleanup, the dynamic filter is safer as it doesn't modify the database, while the database update provides permanent cleanup but requires a backup.