Default sort list for every new Site in your WordPress MultiSite environment

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

WordPress MultiSite environment is a great way to create a network of sites, easily managed from a common dashboard. The Advanced Post Types Order plugin is fully compatible with MultiSite set-up, it can be activated either on punctual site or network wide.

On large networks, running the same plugins and theme on every blog, it can take a while to configure required sorts within every site. This can be automatized for every new/existing blog to include a specific sort, ready to be used by anyone who get access and include the required capability. Imagine providing a new sort functionality for everyone in the network, by allowing to order a newly introduced “Testimonials” custom post type within your theme or plugins.

The following code should be saved within a file and placed inside your mu-plugins folder. If the directory does not exists you can create it within wp-content

First we need to check if the current network site has previously created the required default sort. The default sort existence is being saved within created_apto_default_sort option for later usage :

/**
    * Check if a default sort exists for this blog    
    */
    add_action( 'plugins_loaded', 'msapto_plugins_loaded', 999);
    function msapto_plugins_loaded()
        {
            
            //no need to continue if plugin is not active
            if ( ! is_plugin_active_for_network('advanced-post-types-order/advanced-post-types-order.php'))
                return;
            
            $default_sort   =   get_option('created_apto_default_sort');
            
            if(!empty($default_sort))
                return;
                
            
            //crate the sort
            add_action('init', 'msapto_create_default_sort');
        }

The code also check if the plugin is actually active otherwise it does nothing.

The next code is the function which create the actual sort list within the current blog. Once created, certain options are being assigned to the sort, those should be changed accordingly to actual requirements:

function msapto_create_default_sort()
    {
        $post_data  =   array(
                                'post_type'     =>  'apto_sort',
                                'post_title'    =>  'Testimonials Sort',
                                'post_status'   =>  'publish'
                                );
        $sort_id = wp_insert_post( $post_data );
        
        //add the settings
        $options    =   array(
                        '_title'                        =>  'Testimonials Sort',
                        '_description'                  =>  'Testimonials Sort',
                        '_location'                     =>  'edit.php?post_type=testimonial',
                        '_autosort'                     =>  'yes',
                        '_adminsort'                    =>  'yes',
                        '_pto_interface_sort'           =>  '',
                        '_new_items_to_bottom'          =>  'no',
                        '_show_thumbnails'              =>  'no',
                        '_pagination'                   =>  'no',
                        '_pagination_posts_per_page'    =>  '100',
                        '_pagination_offset_posts'      =>  '5',
                        '_wpml_synchronize'             =>  'no',
                        '_capability'                   =>  'read',
                        
                        '_status'                       =>  array (
                                      'all' => 
                                      array (
                                        'label' => '<b>All</b>',
                                        'status' => 'show',
                                      ),
                                      'publish' => 
                                      array (
                                        'label' => 'Published',
                                        'status' => 'show',
                                      ),
                                      'future' => 
                                      array (
                                        'label' => 'Scheduled',
                                        'status' => 'show',
                                      ),
                                      'draft' => 
                                      array (
                                        'label' => 'Draft',
                                        'status' => 'show',
                                      ),
                                      'pending' => 
                                      array (
                                        'label' => 'Pending',
                                        'status' => 'show',
                                      ),
                                      'private' => 
                                      array (
                                        'label' => 'Private',
                                        'status' => 'show',
                                      ),
                                      'trash' => 
                                      array (
                                        'label' => 'Trash',
                                        'status' => 'show',
                                      ),
                                      'auto-draft' => 
                                      array (
                                        'label' => 'auto-draft',
                                        'status' => 'show',
                                      ),
                                      'inherit' => 
                                      array (
                                        'label' => 'inherit',
                                        'status' => 'show',
                                      )
                                                                      ),
                        'conditionals'     =>  array(),
                        '_view_type'      =>  'multiple',
                        
                        '_rules'          =>  array (
                                                      'post_type' => 
                                                      array (
                                                        0 => 'testimonial',
                                                      ),
                                                      'taxonomy_relation' => 'AND',
                                                      'meta_relation' => 'AND',
                                                    ),
                        
                        '_settings_update_languages'    =>  array (
                                                          'en' => true,
                                                        )
                        );
                                        
        //add the meta
        foreach (  $options as  $option    =>  $value)
            {
                
                update_post_meta($sort_id, $option, $value);   
                
            }
        
        
        //add an option to held a sort previously created
        update_option('created_apto_default_sort', $sort_id);
        
    }

Fore newly created sort, it’s been assigned the testimonial post type. Also the Autosort and Admin sort are turned ON. Obliviously any setting can be set as required, for full list of available options see https://www.nsp-code.com/advanced-post-types-order-description-and-usage/understanding-sort-list-settings-area/