How to order by blog ID in this multisite ‘List Blogs’ custom function

I found this really cool function to list all the blogs on a multisite network install.

Because the original multisite list blogs function was depreciated :/

Read More

So I am using this function below which is great because it is so flexible.

But the only thing I can’t seem to work out is how to re-order the get_results query. Can any one please help me understand how to re-order this query by blog id, instead of the current outputted alphabetical order.

function projects_menu($link_self = true)
{
    global $wpdb;
    echo '<ul class="nav nav-list">';

    $blogs = $wpdb->get_results("
        SELECT blog_id
        FROM {$wpdb->blogs}
        WHERE site_id = '{$wpdb->siteid}'
        AND spam = '0'
        AND deleted = '0'
        AND archived = '0'
        AND blog_id != 1
    ");
    $sites = array();
    foreach ($blogs as $blog) {
        $sites[$blog->blog_id] = get_blog_option($blog->blog_id, 'blogname');
    }
    natsort($sites);
    foreach ($sites as $blog_id => $blog_title) {
        projects_menu_entry($blog_id, $blog_title, $link_self);
    }

    echo '</ul>';
}

Source of this functions https://gist.github.com/davejamesmiller/1966341

Thanks in advance.


See this should work but does not…

$blogs = $wpdb->get_results("
        SELECT blog_id
        FROM {$wpdb->blogs}
        WHERE site_id = '{$wpdb->siteid}'
        AND spam = '0'
        AND deleted = '0'
        AND archived = '0'
        AND blog_id != 1
        ORDER BY blog_id
    ");

// EVENTS SITEMAP
function projects_menu_entry($id, $title, $link_self)
{
    global $blog_id;
    if ($link_self || $id != $blog_id) {
        if ($id == $blog_id) {
            echo '<li class="active">';
        } else {
            echo '<li>';
        }
        $url = get_home_url($id);
        if (substr($url, -1) != '/') {
            // Note: I added a "/" to the end of the URL because WordPress
            // wasn't doing that automatically in v3.0.4. YMMV.
            $url .= '/';
        }
        echo '<a class="notranslate" href="' . $url . '"><i class="icon-folder-close"></i> ' . $title . '</a>';
        echo '</li>';
    }
}
function projects_menu($link_self = true)
{
    global $wpdb;
    echo '<ul class="nav nav-list">';
    echo '<li class="nav-header">Admin sitemap</li>';

    $query = "
    SELECT blog_id
    FROM $wpdb->blogs
    WHERE site_id = %d
       AND public   = '1'
       AND archived = '0'
       AND mature   = '0'
       AND spam     = '0'
       AND deleted  = '0'
       AND blog_id != '1'
    ORDER BY blog_id ASC";
    $blogs = $wpdb->get_col( $wpdb->prepare( $query, $wpdb->siteid ) );

    $sites = array();
    foreach ($blogs as $blog) {
        $sites[$blog->blog_id] = get_blog_option($blog->blog_id, 'blogname');
    }

    foreach ($sites as $blog_id => $blog_title) {
        projects_menu_entry($blog_id, $blog_title, $link_self);
    }

    projects_menu_entry(1, 'Home', $link_self);

    echo '</ul>';

}

Please see above my function ‘trying’ to list all blogs in on my network site and show which is currently being viewed by having a active class.

But the above code is only returning one site. And the home link.

Related posts

Leave a Reply

1 comment

  1. The following query works. Note the quotes in AND blog_id != '1'

    global $wpdb;
    $query = "
    SELECT blog_id
    FROM $wpdb->blogs
    WHERE site_id = %d
        AND public   = '1'
        AND archived = '0'
        AND mature   = '0'
        AND spam     = '0'
        AND deleted  = '0'
        AND blog_id != '1'
    ORDER BY blog_id ASC";
    $blogs = $wpdb->get_col( $wpdb->prepare( $query, $wpdb->siteid) );
    

    Result in a multi-site with three sites:

    blogs = array (
      0 => '2',
      1 => '3',
    )