0

How can I query the latest 3 posts across my entire WordPress multisite network?

For example, BlogA has 2 posts from April 30th, and a post from April 27th, and BlogB has 1 post from April 29th. We would get two posts from BlogA, and a post from BlogB.

1

1 Answer 1

0

The link provided by Christine Cooper has a great answer. There are many ways, and you can look through the plugin WP Latest Posts for code, too.

Start by looking at the get_last_updated function. WordPress provides get_posts as the basis to retrieve posts, even across a network. In addition, take a look at switch_to_blog() to pull from the across the network. The final piece to the puzzle is the setup_postdata for global post data.

This piece of code may be helpful. I apologize, the source is unknown but is not originally mine.

$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();
}

The array in get_posts can help select the conditions for the posts. For example, you may want to look at offset because it must be present for the posts_per_page ( interchangeable with numberposts ) to work. Read over the other conditions, too, such as needing global post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.