如何在我的多站点安装中查询同一类型的所有帖子?

时间:2011-09-09 作者:Mazatec

我有一个多站点wordpress安装,有5个站点,每个站点都有相同的4个自定义帖子类型,

我想查询所有网站上最新的5篇帖子post_typegallery

is it possible to get wp_query() to pull across all blogs within the site instead of just the current blog?

2 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

是,但不是在单个查询中,例如:

if(is_multisite()){
    global $wpdb;
    $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = \'0\' AND deleted = \'0\' and archived = \'0\' and public=\'1\'"));
    if(!empty($blogs)){
        ?><?php
        foreach($blogs as $blog){
            switch_to_blog($blog->blog_id);
            $details = get_blog_details($blog->blog_id);
            $q = new WP_query();
            if($q->have_posts()){
                while($q->have_posts()){
                    $q->the_post();
                    // do things
                    break;
                }
            }
            wp_reset_query();
            restore_current_blog();
        }
    }
}
如果你想在每个博客中显示最新的帖子,那应该很容易。如果你想在整个网络上显示个人的最新帖子,你需要找到每个博客的最新帖子,你需要存储找到的最新帖子,在找到新帖子时替换ti,然后在循环后输出。

SO网友:Creative Choice

切换到博客非常昂贵。。。下面是我使用的一些代码(我使用自己的寻呼机和查询变量)

function recent_mu_posts($howmany, $paged, $when, $until, $search) {
/*  returns array of posts from all blogs in $blogs, newer than $when
*   older than $until, containing $search
*/
  global $options;
  global $wp_query;
  global $blogs;
  global $wpdb;
  global $table_prefix;

  $blogPostTableNames = get_mu_tables();
    if ( count( $blogPostTableNames ) > 0 ) :
      $query = \'\'; $i = 0;
      foreach ( $blogPostTableNames as $blogId => $tableName ) :
        if ( $i > 0 ) :
        $query.= \' UNION \';
        endif;
        $query.= " (SELECT ID, post_date, $blogId as `blog_id` FROM $tableName WHERE post_status = \'publish\' AND post_type = \'post\' AND post_date >= \'$when\' AND post_date <= \'$until\'";
        if (isset($search)) $query.= " AND ( post_content COLLATE UTF8_GENERAL_CI LIKE \'%{$search}%\' OR post_title COLLATE UTF8_GENERAL_CI LIKE \'%{$search}% \')";
        $query.=")";
        $i++;
      endforeach;
      $query.= " ORDER BY post_date DESC ";
      /* pagination */
      $totalposts = $wpdb->get_results($query, OBJECT);
      $ppp = intval( get_query_var( \'posts_per_page\' ) );
      $on_page = $paged; #intval( get_query_var( \'paged\' ) ); 
      if( $on_page == 0 ) $on_page = 1;  
      $offset = ( $on_page - 1 ) * $ppp;
      //set global $wp_query object for pagination

      $wp_query->found_posts = count($totalposts);
      $wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp); 
      $wp_query->request = $query . " LIMIT $ppp OFFSET $offset";

      //= " LIMIT $ppp OFFSET $offset"; 
      $rows = $wpdb->get_results( $wp_query->request, OBJECT);
      if ( $rows ) :
        $posts = array();
        $i=0;
        global $post;
        foreach ( $rows as $post ) :
            $posts[$i] = get_blog_post( $post->blog_id, $post->ID );
            $posts[$i]->blog_id = $post->blog_id;
            $i++;
        endforeach;
        $wp_query->posts = $posts;
        return $posts;
      endif;
  endif;
}

结束

相关推荐

Multisite features

我在考虑创建一个由用户创建的博客组成的网络。我读过一些关于wordpress多站点功能的文章,我想知道这是否是我想要的。。。我需要做的是创建一个网站,让用户注册并创建他们的onw博客。wordpress multisite是否提供此功能?如果不是,手动将此功能实现到wp中是可行的,还是应该从0或其他平台开始?非常感谢。