Filters ‘request’ and ‘parse_query’ not firing in sites.php nor link-manager.php

I managed to order the link-manager.php by a custom column using this:

/* 
 * Sort links using the custom column link_image
 */
function sort_on_field(&$objects, $on, $order="ASC") { 
    $comparer = ($order === 'DESC') 
        ? "return -strcmp(\$a->{$on},\$b->{$on});" 
        : "return strcmp(\$a->{$on},\$b->{$on});"; 
    usort($objects, create_function('$a,$b', $comparer)); 
}

function brsfl_link_manager_order($links) {
    global $current_screen;
    if($current_screen->id == 'link-manager' && $_GET['orderby'] == 'link_image') {
        $order = ($_GET['order'] === 'asc') ? 'ASC' : 'DESC';
        sort_on_field($links, 'link_image', $order);
    } 
    return $links;
} 

if(is_admin()) add_filter('get_bookmarks','brsfl_link_manager_order');

For the sites.php listing, things seem more tricky… I found an interesting code in this thread, but finally made it work with:

public function callback_for_plugins_loaded()
{
    global $pagenow;
    if( 
        is_super_admin() 
        && 'sites.php' == $pagenow
        && isset( $_GET['orderby'] ) && 'site-category' == $_GET['orderby'] 
    ) 
        add_filter( 'query', array( $this, 'filter_site_query' ) );
}

public function filter_site_query( $query )
{
    global $wpdb;
    $search_query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '1'  LIMIT 0, 20";

    if( isset( $_GET['order'] ) )
        $order = ( 'asc' == $_GET['order'] ) ? 'ASC' : 'DESC';
    else
        $order="DESC";

    if( strpos( $query, $search_query ) !== FALSE )
        $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '1'  ORDER BY mature $order LIMIT 0, 20";

    return $query;
}

I’m using the column wp_blogs.mature as an index for site categories. I guess more complex queries can be done, but right now that column seems allright.

Leave a Comment