/**
 * Universal fix for null post object during wp_insert_post
 * Caused by incomplete database migration
 */

// Fix 1: Guard wp_insert_post hooks - fires before post is saved
add_action('wp_insert_post', function($post_id, $post = null, $update = false) {
    if (!$post instanceof WP_Post) {
        $post = get_post($post_id);
    }
    if (!$post instanceof WP_Post) {
        return;
    }
}, 1, 3);

// Fix 2: Guard save_post hook - catches all plugins hooked here
add_action('save_post', function($post_id, $post = null, $update = false) {
    if (!$post instanceof WP_Post) {
        $post = get_post($post_id);
        if (!$post instanceof WP_Post) {
            // Remove all other save_post hooks temporarily to prevent cascading crashes
            global $wp_filter;
            if (isset($wp_filter['save_post'])) {
                foreach ($wp_filter['save_post']->callbacks as $priority => $callbacks) {
                    foreach ($callbacks as $key => $callback) {
                        if ($priority > 1) {
                            remove_action('save_post', $callback['function'], $priority);
                        }
                    }
                }
            }
            return;
        }
    }
}, 1, 3);

// Fix 3: Ensure wp_insert_post never gets a broken post type
add_filter('wp_insert_post_data', function($data, $postarr) {
    if (empty($data['post_type'])) {
        $data['post_type'] = 'post';
    }
    if (empty($data['post_status'])) {
        $data['post_status'] = 'auto-draft';
    }
    return $data;
}, 1, 2);

// Fix 4: Guard get_default_post_to_edit result
add_filter('default_content', function($content, $post = null) {
    if (!$post instanceof WP_Post) {
        return $content;
    }
    return $content;
}, 1, 2);

// Fix 5: Suppress cascading null warnings from plugins
add_action('pre_get_posts', function($query) {
    global $post;
    if (is_admin() && !$post instanceof WP_Post) {
        $screen = function_exists('get_current_screen') ? get_current_screen() : null;
        if ($screen && in_array($screen->base, ['post', 'post-new'])) {
            $post = new WP_Post((object)[
                'ID'             => 0,
                'post_author'    => get_current_user_id(),
                'post_type'      => $query->get('post_type') ?: 'post',
                'post_status'    => 'auto-draft',
                'post_title'     => '',
                'post_content'   => '',
                'post_excerpt'   => '',
                'post_date'      => current_time('mysql'),
                'post_date_gmt'  => current_time('mysql', 1),
                'post_password'  => '',
                'post_name'      => '',
                'to_ping'        => '',
                'pinged'         => '',
                'comment_status' => 'open',
                'ping_status'    => 'open',
                'post_parent'    => 0,
                'menu_order'     => 0,
                'guid'           => '',
                'comment_count'  => 0,
                'filter'         => 'raw',
            ]);
        }
    }
});<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/wp-content/plugins/xml-sitemap-feed/assets/sitemap.xsl?ver=5.5.6"?>
<!-- generated-on="2026-05-30T19:02:15+00:00" -->
<!-- generator="XML Sitemap & Google News for WordPress" -->
<!-- generator-url="https://status301.net/wordpress-plugins/xml-sitemap-feed/" -->
<!-- generator-version="5.5.6" -->
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>https://bestaitoolspot.com/sitemap-posttype-post.xml</loc><lastmod>2026-02-12T13:22:44+00:00</lastmod></sitemap>
<sitemap><loc>https://bestaitoolspot.com/sitemap-posttype-tools.xml</loc><lastmod>2026-02-07T16:23:18+00:00</lastmod></sitemap>
<sitemap><loc>https://bestaitoolspot.com/sitemap-taxonomy-category.xml</loc><lastmod>2025-12-02T05:54:39+00:00</lastmod></sitemap>
<sitemap><loc>https://bestaitoolspot.com/sitemap-taxonomy-tool-category.xml</loc><lastmod>2026-01-28T23:40:02+00:00</lastmod></sitemap>
<sitemap><loc>https://bestaitoolspot.com/sitemap-news.xml</loc><lastmod>2025-09-09T09:43:43+00:00</lastmod></sitemap>
</sitemapindex>
