Sort by site/blog column in Multisite

I use WordPress Multisite and made a plugin that adds columns with some data. My code is based on this WP Engineer article.

The key function in WordPress Multisite is manage_sites_custom_column. And the problem is that I want to be able to sort the column I created.

Read More

I tried the code in this Scribu’s article but it is based on manage_posts_custom_column for single sites.

Related posts

Leave a Reply

2 comments

  1. I tried to solve this issue before. Taking a second look at it, we can alter the sites list using the query filter hook.

    Supposing our custom sort URL is
    http://example.com/wp-admin/network/sites.php?orderby=site_category

    add_action( 'plugins_loaded', function()
    {
        global $pagenow;
        if( 
            is_super_admin() 
            && 'sites.php' == $pagenow
            && isset( $_GET['orderby'] ) 
            && 'site_category' == $_GET['orderby'] 
        ) 
            add_filter( 'query', 'b5f_filter_site_query' );
    });
    
    function b5f_filter_site_query( $query )
    {
        global $wpdb;
        if( FALSE !== strpos( $query, "SELECT * FROM {$wpdb->blogs} WHERE site_id = '1'  LIMIT 0, 20" ) )
            $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '1'  ORDER BY mature DESC LIMIT 0, 20";
        return $query;
    }
    

    Yes, I’m playing with the mature column. Probably, a refined SQL query could be made, but not my league… This column is a TINYINT(2). WordPress recognizes 0 and 1, leaving us with 98 options. In my case, enough to categorizing.

    If mature > 1, the site is marked as such in site-info.php, but not in sites.php. I’m not really sure how being mature affects the site output.
    A workaround to use the mature column liberally is to tell WordPress everything is ok, don’t worry, outside the sites.php screen:

    add_action( 'plugins_loaded', function()
    {
        global $pagenow;
        if( 'sites.php' != $pagenow )
            add_filter( 'blog_details', 'b5f_hack_mature_queries' );
    
        # Hide the Mature checkbox in site-info.php
        add_action( 'admin_footer', 'b5f_print_sites_footer' );
    });
    
    function b5f_hack_mature_queries( $details )
    {
        $details->mature = 0;
        return $details;
    }
    
    function b5f_print_sites_footer()
    {
        if( 'site-info-network' != get_current_screen()->id )
            return;
    
    echo <<<HTML
        <script type="text/javascript">
            jQuery(document).ready( function($) {
                $(".form-table").find("label:contains('Mature')").remove();
            });
        </script>
    HTML;
    }
    
  2. Not a full answer… but for others searching for how to alter the sortability of the “sites” page.

    The following does not work as expected:

    add_filter('manage_sites_sortable_columns', ...
    

    Instead, try:

    add_filter('manage_sites-network_sortable_columns', ...