I have create wordpress mu in my localhost. I put there all my code / work project / tutorial. I seperate them into several sub domain blog. Thanks to wordpress MU, i can make them on single domain. But sometimes, i have problem seek the information i need. So i create this code to help me populate all post in the blog.
You have to put this on a template page in your theme.
This code has ability to sort post based on post_title. Thanks to msort function which has ability to sort array. By the way, msort function from php.net and jachim.
//sorting double array based on key alphabet
function msort($array, $id="id", $sort_ascending=true) {
$temp_array = array();
while(count($array)>0) {
$lowest_id = 0;
$index=0;
foreach ($array as $item) {
if (isset($item[$id])) {
if ($array[$lowest_id][$id]) {
if (strtolower($item[$id]) < strtolower($array[$lowest_id][$id])) {
$lowest_id = $index;
}
}
}
$index++;
}
$temp_array[] = $array[$lowest_id];
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
}
if ($sort_ascending) {
return $temp_array;
} else {
return array_reverse($temp_array);
}
}
function get_blog_post_mimic ($blog_id) {
global $wpdb;
$table_blog_id = "{$wpdb->base_prefix}{$blog_id}_posts ";
$query = "SELECT guid, post_title FROM " . $table_blog_id . " WHERE post_type = ‘post’ ORDER BY post_title";
$posts = $wpdb->get_results($query, ARRAY_A );
return $posts;
}
/* FOR : merge all query result of existing blog in wpmu database
WHY : To post all posting into one blog
HOW : get all blog id, ilumerate $listblogs,
get array contain all post on each blog
merge each array into single array ($allpost)
sort $allpost based on post_title array key
show all single post and its permalink
*/
function show_all_post_in_WPMU($do_sorting = FALSE){
global $wpdb;
$listblogs = get_blog_list(0,0);
$listblogs = msort( $listblogs, "blog_id" );
unset($listblogs[0]); //for : erase the main blog from the list. It’s blog id = 1
if( is_array( $listblogs ) ) {
$allpost[] = array();
$number = 1;
foreach( $listblogs as $details ) {
$blog_id = $details['blog_id'];
$myposts = get_blog_post_mimic($blog_id);
if (is_array($myposts)) {
$allpost = array_merge($myposts, $allpost);
}
}
if ($do_sorting) {
$allpost = msort( $allpost, "post_title" );
}
foreach ($allpost as $post) {
$judul .= "<li><a href =’" . $post['guid'] . "’ target=’_blank’>" . $post['post_title'] . "</a></li>";
}
echo "<h2>List of all POST </h2><ol>" . $judul . "</ol>";
}
}
$page_url = get_permalink();
echo "<a href =’" . $page_url . "/?dosorting=true’>Do Sorting</a> | ";
echo "<a href =’" . $page_url . "/’>No Sorting</a>";
if ($_GET['dosorting'] == ‘true’) {
show_all_post_in_WPMU(TRUE);
} else {
show_all_post_in_WPMU();
}
Tags: Template Page, Wordpress, Wordpress MU
One Response to “Howto Show All Post on WordPress MU Blogs”
Leave a Reply. Btw, i have right to delete your comment. Sometimes, i need to save my databse space.
wowo, this is something i need. Thanks bro, hopefully you can make this snippet better. For example, how about a better query from more the one database
is it too much