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:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x