Pagination for Multisite Posts
This tutorial is kind of documentation for my WordPress Multisite plugin and its class Network_Query
which allows to get posts from all blogs in your multisite network within one single query.
This tutorial consists from two simple steps. In the first step I will show how to create a custom pagination function and in the second one โ we will work with posts loop.
Let’s begin.
1. How to Use paginate_links() WordPress Function to Create Any Kind of Pagination
It is really great that WordPress has paginate_links()
function in its core since 2.1.0 version.
In this step we create a wrapper for it, for example we can call it misha_paginate_links()
but we need it just to simplify the whole process, so it is not necessary and you can pass the parameters directly to paginate_links()
functions.
Please insert this code to your current (or child) theme functions.php
file.
/*
* @description paginate_links() wrapper
* @author Misha Rudrastyh
* @url https://rudrastyh.com/wordpress/multisite-posts-pagination.html
*/
if( ! function_exists( 'misha_paginate_links' ) ) {
function misha_paginate_links( $query ) {
$search_page_url = 'YOUR_SEARH_PAGE_URL'; // please fill this parameter
$args = array(
'total' => $query->max_num_pages, // total amount of pages
'current' => ( ( $query->query_vars[ 'paged' ] ) ? $query->query_vars[ 'paged' ] : 1 ), // current page number
'show_all' => false, // set to true if you want to show all pages at once
'mid_size' => 2, // how much page numbers to show on the each side of the current page
'end_size' => 2, // how much page numbers to show at the beginning and at the end of the list
'prev_next' => true, // if you set this to false, the previous and the next post links will be removed
'prev_text' => '«', // ยซ
'next_text' => '»', // ยป
'base' => $search_page_url . '%_%',
'format' => '?current_page=%#%'
);
if( $args[ 'total' ] <= 1 ) { // do not return anything if there are not enough posts
return;
}
return '<div class="navigation">
<span class="pages">Page ' . $args[ 'current' ] . ' of ' . $args[ 'total' ] . '</span>'
. paginate_links( $args ) .
'</div>';
}
}
Parameters base
and format
are not necessary by the way, but allow to prevent 404 error in some cases.
All the parameters detailed description you can find in official documentation.
2. Get Multisite Posts with Pagination. Working with the Loop
The code below with the help of Network_Query allows you to display the posts (their titles and permalinks) from all the blogs in a multisite network.
Where to insert it? The best way โ inside a custom page template.
// get the current page number from the URL parameter, i.e. ?current_page=2
$current_page = ( isset( $_GET[ 'current_page' ] ) && $_GET[ 'current_page' ] ) ? $_GET[ 'current_page' ] : 1;
// this class is similar to WP_Query but works for multisite posts, my plugin is required, the link is above in this post
$multisite_query = new Network_Query(
array(
'posts_per_page' => 1,
'paged' => $current_page
)
);
// run the loop
if( $multisite_query->have_posts() ) :
while( $multisite_query->have_posts() ) : $multisite_query->the_post();
// we need switch_to_blog() to work with get_permalink() and some other loop functions
switch_to_blog( $multisite_query->post->BLOG_ID );
echo '<p><a href="' . get_permalink( $multisite_query->post->ID ) . '">' . $multisite_query->post->post_title . '</a></p>';
// switch back
restore_current_blog();
endwhile;
// print pagination, pass the query object as a required parameter
echo misha_paginate_links( $multisite_query );
endif;
network_reset_postdata();

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Thanks for the wonderful code. I need to display posts from all multisite blogs in the back-end, so that the editor can manage the posts in the wp-admin area like in the single site. How can we do it?
Hi again,
Anyway, I’ve added this feature. More info is here. No ACF columns are supported, but actually you can edit plugin’s
posts.php
file a little for your needs.Hi Misha,
Thanks for this amazing plugin. I’m excited to implement it on my site.
I’m having trouble getting the pagination working, though. As you suggested, I’m using a custom page template and have it set as my front page under Settings > Reading. On that custom page, I copied and pasted the code you have displayed above. I also copied the misha_pagination function into my functions.php file.
While I do see the page number links at the bottom of my home page, clicking any of them just displays same first 10 posts. I deactivated all other plugins and even tried this using the twentyten theme to keep it simple, but I’m having no luck. Any suggestions?
Nevermind. I just saw your comment in the code.
// (!) if you use the code on the front page, change PAGED to PAGE, e.g. get_query_var(‘page’)
That fixed it.
Hi,
Ok, I’m glad you’ve figured it out ๐
I see on your site that Network_Query will accept all of the same arguments as WP_Query, but I’m having trouble getting the network query to exclude sticky posts. The code I’m using is below, but sticky posts are still being included. Can you help?
Hi,
get_option( 'sticky_posts' )
returns only sticky posts from your current blog/site where you use the query ๐Is there a way that I could add an argument to the Network_Query that would exclude sticky posts from the results?
Yes, actually you can use the same
post__not_in
, but find a way to add sticky posts in one array common for all blogs.Or just use
'ignore_sticky_posts' => true
.