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

I'm trying to make sortable columns in Sites and Links listings admin pages.

/wp-admin/network/sites.php and /wp-admin/link-manager.php

The problem is the following filters are not firing up in these screens, preventing the query modification. Is there other method that could be used?

function site_category_column_orderby( $vars ) {
    //global $firephp;
    //$firephp->log($vars, 'vars');
    if ( isset( $vars['orderby'] ) && 'site_category' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'site_category',
            'orderby' => 'meta_value_num'
        ) );
    }
    return $vars;
}
add_filter( 'parse_query', 'site_category_column_orderby' );


function link_thumbnail_column_orderby( $vars ) {
    if ( isset( $vars['orderby'] ) && 'link_thumbnail' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'link_thumbnail',
            'orderby' => 'meta_value_num'
        ) );
    }
    return $vars;
}
add_filter( 'request', 'link_thumbnail_column_orderby' );

[UPDATE]

I'd like to sort the a Link column for this plugin: https://wordpress.stackexchange.com/a/50389/12615

And the a Site column for this other one: https://wordpress.stackexchange.com/a/50936/12615

Answers 1

  • 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.


Related Questions