WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Recent Posts From Whole Network (5 posts)

  1. myladeybugg
    Member
    Posted 1 year ago #

    I have multisite with buddypress installed and am looking to display some of the recent posts from across my network and found this tutorial: http://wp.smashingmagazine.com/2011/11/17/wordpress-multisite-practical-functions-methods/comment-page-1/#comment-74424

    I am getting error with one of my foreach loops and can't seem to figure out how to fix it.

    Here's the code I have so far:

    <?php
    function wp_recent_across_network( $size = 10, $expires = 7200 ) {
       if( !is_multisite() ) return false;
    
       // Cache the results with the WordPress Transients API
       // Get any existing copy of our transient data
       if ( ( $recent_across_network = get_site_transient( 'recent_across_network' ) ) === false ) {
    
          // No transient found, regenerate the data and save a new transient
          // Prepare the SQL query with $wpdb
          global $wpdb;
    
          $base_prefix = $wpdb->get_blog_prefix(0);
          $base_prefix = str_replace( '1_', '' , $base_prefix );
    
          // Because the get_blog_list() function is currently flagged as deprecated
          // due to the potential for high consumption of resources, we'll use
          // $wpdb to roll out our own SQL query instead. Because the query can be
          // memory-intensive, we'll store the results using the Transients API
          if ( false === ( $site_list = get_site_transient( 'multisite_site_list' ) ) ) {
             global $wpdb;
             $site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY id = %d, $id') );
             set_site_transient( 'multisite_site_list', $site_list, $expires );
          }
    
          $limit = absint($size);
    
          // Merge the wp_posts results from all Multisite websites into a single result with MySQL "UNION"
          foreach ( $site_list as $site ) {
             if( $site == $site_list[0] ) {
                $posts_table = $base_prefix . "posts";
             } else {
                $posts_table = $base_prefix . $site->blog_id . "_posts";
             }
    
             $posts_table = esc_sql( $posts_table );
             $blogs_table = esc_sql( $base_prefix . 'blogs' );
    
             $query .= "(SELECT $posts_table.ID, $posts_table.post_title, $posts_table.post_date, $blogs_table.blog_id FROM $posts_table, $blogs_table\n";
             $query .= "\tWHERE $posts_table.post_type = 'post'\n";
             $query .= "\tAND $posts_table.post_status = 'publish'\n";
             $query .= "\tAND $blogs_table.blog_id = {$site->blog_id})\n";
    
             if( $site !== end($site_list) )
                $query .= "UNION\n";
             else
                $query .= "ORDER BY post_date DESC LIMIT 0, $limit";
          }
    
          // Sanitize and run the query
          $query = $wpdb->prepare($query);
          $recent_across_network = $wpdb->get_results( $query );
    
          // Set the Transients cache to expire every two hours
          set_site_transient( 'recent_across_network', $recent_across_network, 60*60*2 );
       }
    
       // Format the HTML output
    
     $html = '<ul>';
       foreach ( $recent_across_network as $post ) {
          $html .= '<li><a>blog_id $post->ID ) . \'">' . $post->post_title . '</a></li>';
       }
       $html .= '</ul>';
    
       return $html;
    }
    ?>
  2. And what error are you getting? :)

  3. myladeybugg
    Member
    Posted 1 year ago #

    I guess that would of helped. :)
    here's the error:
    Warning: Invalid argument supplied for foreach() in /home/public_html/domain.com/i/wp-content/themes/bp-child/functions.php on line 119

    Line 119 is this:

    foreach ( $recent_across_network as $post ) {
          $html .= '<li><a>blog_id, $post->ID ) . \'">' . $post->post_title . '</a></li>';
       }

    I changed a little bit of code to fix a different error so here's my current code:

    /**
     * List recent posts across a Multisite network
     *
     * @uses get_blog_list(), get_blog_permalink()
     *
     * @param int $size The number of results to retrieve
     * @param int $expires Seconds until the transient cache expires
     * @return object Contains the blog_id, post_id, post_date and post_title
     */
    function wp_recent_across_network( $size = 10, $expires = 7200 ) {
       if( !is_multisite() ) return false;
    
       // Cache the results with the WordPress Transients API
       // Get any existing copy of our transient data
       if ( ( $recent_across_network = get_site_transient( 'recent_across_network' ) ) === false ) {
    
          // No transient found, regenerate the data and save a new transient
          // Prepare the SQL query with $wpdb
          global $wpdb;
    
          $base_prefix = $wpdb->get_blog_prefix(0);
          $base_prefix = str_replace( '1_', '' , $base_prefix );
    
          // Because the get_blog_list() function is currently flagged as deprecated
          // due to the potential for high consumption of resources, we'll use
          // $wpdb to roll out our own SQL query instead. Because the query can be
          // memory-intensive, we'll store the results using the Transients API
          if ( false === ( $site_list = get_site_transient( 'multisite_site_list' ) ) ) {
             global $wpdb;
             $site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY blog_id') );
             set_site_transient( 'multisite_site_list', $site_list, $expires );
          }
    
          $limit = absint($size);
    
          // Merge the wp_posts results from all Multisite websites into a single result with MySQL "UNION"
          foreach ( $site_list as $site ) {
             if( $site == $site_list[0] ) {
                $posts_table = $base_prefix . "posts";
             } else {
                $posts_table = $base_prefix . $site->blog_id . "_posts";
             }
    
             $posts_table = esc_sql( $posts_table );
             $blogs_table = esc_sql( $base_prefix . 'blogs' );
    
             $query .= "(SELECT $posts_table.ID, $posts_table.post_title, $posts_table.post_date, $blogs_table.blog_id FROM $posts_table, $blogs_table\n";
             $query .= "\tWHERE $posts_table.post_type = 'post'\n";
             $query .= "\tAND $posts_table.post_status = 'publish'\n";
             $query .= "\tAND $blogs_table.blog_id = {$site->blog_id})\n";
    
             if( $site !== end($site_list) )
                $query .= "UNION\n";
             else
                $query .= "ORDER BY post_date DESC LIMIT 0, $limit";
          }
    
          // Sanitize and run the query
          $query = $wpdb->prepare($query);
          $recent_across_network = $wpdb->get_results( $query );
    
          // Set the Transients cache to expire every two hours
          set_site_transient( 'recent_across_network', $recent_across_network, 60*60*2 );
       }
    
       // Format the HTML output
       $html = '<ul>';
       foreach ( $recent_across_network as $post ) {
          $html .= '<li><a>blog_id, $post->ID ) . \'">' . $post->post_title . '</a></li>';
       }
       $html .= '</ul>';
    
       return $html;
    }
  4. I thought that BuddyPress (it's been about a year since I used it extensively) collected all your posts etc in the /activity section?

    Couldn't you use the activity loop instead? http://codex.buddypress.org/developer/developer-docs/loops-reference/the-activity-stream-loop/

  5. myladeybugg
    Member
    Posted 1 year ago #

    Yeah it does collect them in the activity stream, but it collect everything such as group activity, profile picture changes, new registered members, etc.

    I was just looking to display blog posts only in my footer. I've looked into the activity loop and got a little something working, just need to configure it a lot more.

    I also found this code which seems to work:

    <!-- Recent Posts -->
    <?php
    $blogs = get_last_updated();
    
    foreach ($blogs AS $blog) {
    
    switch_to_blog($blog["blog_id"]);
    $lastposts = get_posts('numberposts=1');
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>
    
    <a href="<?php the_permalink(); ?>" style='color:white !important;'><?php the_author(); ?></a>
    
     - <a href="<?php the_permalink(); ?>" style='color:white !important; margin-bottom: 5px;'><?php the_title(); ?></a></li></ul> <br />
    
    <?php
    endforeach;
    restore_current_blog();
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic