Site icon WP Smith

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]