调用WordPress mu站点群中的所有站点的最新文章(自定义类型)

函数代码:

function recent_posts_mu($posttype = 'post',$number = 20)
{
    global $wpdb;
    $blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
			public = '1' AND blog_id !='1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'
			ORDER BY last_updated DESC");
    if ($blogs) {
        foreach ($blogs as $blog) {
            $blogPostsTable = $wpdb->base_prefix . $blog . "_posts";

            $wpdb->query("
					CREATE TEMPORARY TABLE IF NOT EXISTS temp_table(
						SELECT *
						FROM 
							$blogPostsTable
						WHERE 
							$blogPostsTable.post_status = 'publish' 
						AND 
							$blogPostsTable.ID > 1
						ORDER BY id DESC
					)
					");

        }
        $thisposts = $wpdb->get_results("
					SELECT *
					FROM temp_table
					WHERE 
						post_type = '" . $posttype . "'
					ORDER BY post_date DESC
					LIMIT 0," . $number . "
				");
        return $thisposts;

    }
}

使用方法:

<?php
    $posts = wpmu_recent_posts_mu('post',10);
    foreach ($posts as $post):
    $blogURL = getBaseDomain($post->guid)->domain;
    $site_id = get_blog_id_from_url($blogURL);
?>

    <li>
        <a href="<?php echo get_blog_permalink($site_id,$post->ID) ?>" ><?php echo $post->post_title; ?></a>
        <span><?php echo date("m-d",strtotime($post->post_date)); ?></span>
    </li>
<?php endforeach;?>