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.

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

Misha Rudrastyh

I develop websites since 2008, so it is total of 14 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me

Follow me on Twitter