Turn off trackback/pingbacks and comments on all existing multisite sites

I want to turn of trackbacks and pingbacks. Is there a way to turn them off on all existing posts and pages on all sites within a multi-site install? Then set the option not to allow trackback and pingsbacks in the setting for each existing sites?

Related posts

Leave a Reply

1 comment

  1. This code was tested in a local testing Multisite. Seems ok, but found a minor glitch:
    – after running it all drafts/pending posts disappear from the listing page, but appear again after a refresh, no idea why…

    Although I believe it’s pretty harmless, please backup your database before running this

    /*
     * Turn off trackback/pingbacks and comments on all existing multisite sites
     *
     * Important: Run Only Once, visit any admin page and delete/disable the hook 
     * 
     */
    add_action('admin_init','wpse_55209_run_only_once');
    function wpse_55209_run_only_once()
    {   
        global $wpdb;
        $blogs = $wpdb->get_results("
            SELECT blog_id
            FROM {$wpdb->blogs}
            WHERE site_id = '{$wpdb->siteid}'
            AND spam = '0'
            AND deleted = '0'
            AND archived = '0'
            AND mature = '0' 
            AND public = '1'
        ");
    
        foreach ($blogs as $blog) 
        {
            update_blog_option($blog->blog_id, 'default_ping_status', 'closed');
            update_blog_option($blog->blog_id, 'default_pingback_flag', 'closed');
            update_blog_option($blog->blog_id, 'default_comment_status', 'closed');
            switch_to_blog( $blog->blog_id );
            $wpdb->set_blog_id($blog->blog_id);
            $allposts = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_status != 'inherit'" );
            foreach( $allposts as $pt )
            {
                $my_post = array();
                $my_post['ID'] = $pt->ID;
                $my_post['comment_status'] = 'closed';
                $my_post['ping_status'] = 'closed';
                wp_update_post( $my_post );
            }
        }
    }