I need to list recent 25 blogs in the network with their title. How to edit this code ?

$bcount = get_blog_count();

  global $wpdb;
  $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'"));
?><ul class="menu"><?php
foreach($blogs as $blog){
    $details = get_blog_details($blog->blog_id);
    if($details != false){
        $addr = $details->siteurl;
        $name = $details->blogname;
        if(!(($blog->blog_id == 1)&&($show_main != 1))){
            <li class="menu-item<?php if($counter == get_current_blog_id()){ echo ' current-menu-item';}?>">
                <a href="<?php echo $addr; ?>"><?php echo $name;?></a>
  • If you're stealing someone else code, at least be so kind to give proper credit. You're not an author of the code in your question. It comes from this answer to this question.
    – trejder
    Commented Nov 9, 2012 at 21:15

1 Answer 1


You just have to modify your query to get only first 25 matches SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1' limit 25.

The result is already order by id what is incremented automatically, you don't have anything to bother about there.

Also please note, it is not a good practice to do such queries on every request, you should cache them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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