Default sort list for every new Site in your WordPress MultiSite environment
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/
Very useful plugin!! I’m trying to add a default sort on a multisite. The above code creates the sort on just one of the sites, not all of them. In the code, should I be iterating through each site to create the default sort for all sites?
That’s correct, the above code will create a new sort for all new sites in the network. If you need to process all existing blogs, you should iterate through each one and run the msapto_create_default_sort() after switched to the site.
what should the _rules settings be with with some meta? Like in the image:
Also, do I iterate through the existing blogs using the action listed above? I’m having trouble adding a sort programmatically to each blog’s post with the meta data in the image.
Try using the following :
‘_rules’ => array (
[post_type] => Array
(
[0] => post
)
[taxonomy_relation] => AND
[meta] => Array
(
[1] => Array
(
[key] => _post-expiration-enabled
[value_type] => string
[value] => 0
[compare] => =
[type] => CHAR
)
)
[meta_relation] => AND
}
To iterate through all existing sites in the Network, i suggest to use get_sites(), more details at https://developer.wordpress.org/reference/functions/get_sites/
This code is creating my apto_sort post, but it does not show in the tabs when I go to reorder the post. Occasionally (twice) it has shown up, but I deleted them for testing purposes. When I have seen the sort show, it does have the right settings. But any reason why the sort would not show every time. I do know the post is in there and the data in the post looks corrrect.
Here is my code:
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(‘advanced-post-types-order/advanced-post-types-order.php’) ) {
add_action(‘init’, ‘msapto_create_default_sort’);
}
}
function msapto_create_default_sort()
{
$default_sort = get_option(‘created_apto_default_sort’);
if(empty($default_sort)) {
$post_data = array(
‘post_type’ => ‘apto_sort’,
‘post_title’ => ‘Department Page Sort’,
‘post_status’ => ‘publish’
);
$sort_id = wp_insert_post( $post_data );
}
else {
$sort_id = $default_sort;
}
//add/change the settings
$options = array(
‘_title’ => ‘Department Page Sort’,
‘_description’ => ‘Department Page Sort’,
‘_location’ => ‘edit.php?post_type=post’,
‘_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’ => ‘editor’,
‘_status’ => array (
‘all’ =>
array (
‘label’ => ‘All‘,
‘status’ => ‘hide’,
),
‘publish’ =>
array (
‘label’ => ‘Published’,
‘status’ => ‘show’,
),
‘future’ =>
array (
‘label’ => ‘Scheduled’,
‘status’ => ‘show’,
),
‘draft’ =>
array (
‘label’ => ‘Draft’,
‘status’ => ‘hide’,
),
‘pending’ =>
array (
‘label’ => ‘Pending’,
‘status’ => ‘hide’,
),
‘private’ =>
array (
‘label’ => ‘Private’,
‘status’ => ‘hide’,
),
‘trash’ =>
array (
‘label’ => ‘Trash’,
‘status’ => ‘hide’,
),
‘auto-draft’ =>
array (
‘label’ => ‘auto-draft’,
‘status’ => ‘hide’,
),
‘inherit’ =>
array (
‘label’ => ‘inherit’,
‘status’ => ‘hide’,
)
),
‘conditionals’ => array(),
‘_view_type’ => ‘multiple’,
‘_rules’ => array (
‘post_type’ =>
array (
0 => ‘post’,
),
‘taxonomy_relation’ => ‘AND’,
‘meta’ =>
array (
1 =>
array (
‘key’ => ‘_post-expiration-enabled’,
‘value_type’ => ‘string’,
‘value’ => ‘0’,
‘compare’ => ‘=’,
‘type’ => ‘CHAR’,
),
),
‘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);
}