WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Feb 17 2011

How to Write a Simple get_ms_sites() Function

This will return an array of sites from WordPress Multisite.

Simple Version

[php]function get_ms_sites() {
global $wpdb;

$query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
$query .= " ORDER BY {$wpdb->blogs}.blog_id ";
$order = "ASC";
$query .= $order;
$sql = $wpdb->prepare($query);
$blog_list = $wpdb->get_results( $sql, ARRAY_A );

return $blog_list;
}
[/php]

"Complicated Version"

[php]function get_ms_sites() {
global $wpdb;
$include_public_only = true; // 1 is public; 0 is private
$include_archived = false; // 1 is archived;0 is not
$include_mature = false; // 1 is mature;0 is not
$include_spam = false; // 1 is spam;0 is not
$include_deleted = false; // 1 is deleted;0 is not

$query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
//Options
$query .= " AND {$wpdb->blogs}.public = ". (($include_public_only) ? "'1'" : "'0'");
$query .= " AND {$wpdb->blogs}.archived = ". (($include_archived) ? "'1'" : "'0'");
$query .= " AND {$wpdb->blogs}.mature = ". (($include_mature) ? "'1'" : "'0'");
$query .= " AND {$wpdb->blogs}.spam = ". (($include_spam) ? "'1'" : "'0'");
$query .= " AND {$wpdb->blogs}.deleted = ". (($include_deleted) ? "'1'" : "'0'");

//Query Order
$qorder = " ORDER BY {$wpdb->blogs}.blog_id "; //Next lines contain other options
//$qorder = " ORDER BY last_updated ";
//$qorder = " ORDER BY domain ";
$query .= $qorder;

//Order ASC = ascending; DESC = descending;
$order.= "ASC"; //can also be "DESC"
$query .= $order;
$sql = $wpdb->prepare($query);
$blog_list = $wpdb->get_results( $sql, ARRAY_A );

return $blog_list;
}[/php]

Written by Travis Smith · Categorized: Tutorials

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in