Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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: http://wordpress.stackexchange.com/a/50389/12615

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

share|improve this question

1 Answer

up vote 0 down vote accepted

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 got stuck in this piece

function SortableCategoryColumn_init(){
    if(is_super_admin()){
        if($_SERVER['PHP_SELF']=='/wp-admin/network/sites.php'){
            if((isset($_GET['orderby'])) && ($_GET['orderby']=="site_category")){
                add_action('query', 'SortableCategoryColumn_sites_query' );
            }
        }
    }
}
function SortableCategoryColumn_sites_query($query){
    global $wpdb,$wp_list_table;
    if(strpos($query,"SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ")!==FALSE){
        $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ORDER BY domain";//.SortableCategoryColumn::query_order_by();
    }
    return $query;
}

add_action('init' ,'SortableCategoryColumn_init');

Either I have to somewhat modify this query to make it order by wp_SITEID_options.option_name->option_value (question for StackOverflow) or I'll have to give that field wp_blogs.mature another use :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.