Ready-to-Use Page Templates to Get Posts

In this article you will find different page template examples of how you can use the Network_Query class which is a part of my Multisite Indexer plugin.

You can either use the code snippets below to create templates in your theme by yourself or, of course, you can download them as a zip file, unarchive and then just copy to your theme folder (or to /page-templates one whatever).

1. Get latest network posts

<?php
/*
 * Template name: Multisite Latest Posts
 */

// you may want or may not want to use the header and footer of your theme
// get_header();

$query = new Network_Query( array(
	'posts_per_page' => 5,
	'post_status' => 'publish',
) );

if( $query->have_posts() ) :

	while( $query->have_posts() ) : $query->the_post();
 		// we need "switch_to_blog()" in order to freely use blog-specific functions
		switch_to_blog( $query->post->BLOG_ID );

		?><h2><a href="<?php echo get_permalink( $query->post->ID ) ?>"><?php echo get_the_title( $query->post->ID ) ?></a></h2><?php

		restore_current_blog();

	endwhile;

else :
	?><p>Nothing found</p><?php
endif;

// you may want or may not want to use the header and footer of your theme
// get_footer();

A couple of moments related to this template:

Page template file:

Download multisite-latest-posts.php

2. Get latest network posts with pagination

Below is a similar example but with the pagination and the redefined global $post variable.

<?php
/*
 * Template name: Multisite Latest Posts with Pagination
 */

// you may want or may not want to use the header and footer of your theme
// get_header();

// getting the current page from the URL
$current = ( isset( $_GET[ 'current_page' ] ) && $_GET[ 'current_page' ] ) ? absint( $_GET[ 'current_page' ] ) : 1;

$query = new Network_Query( array(
	'posts_per_page' => 2,
	'post_status' => 'publish',
	'paged' => $current, // we pass a current page value here
) );

if( $query->have_posts() ) :

	while( $query->have_posts() ) : $query->the_post();

		switch_to_blog( $query->post->BLOG_ID );

		// redefine the global $post variable in order to use the_permalink() and the_title()
		global $post;
		$post = $query->post;

		?><h2><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h2><?php

		restore_current_blog();

	endwhile;

	// pagination here
	if( $query->max_num_pages > 1 ) {
		echo paginate_links(
			array(
				'total' => $query->max_num_pages,
				'current' => $current,
				'base' => 'https://test.rudrastyh.com/search-page/%_%',
				'format' => '?current_page=%#%'
			)
		);
	}

else :
	?><p>Nothing found</p><?php
endif;

// you may want or may not want to use the header and footer of your theme
// get_footer();

Some notes:

The page template file is below:

Download multisite-latest-posts-w-pagination.php

3. Search results page

Searching posts across multiple sites of the network is probably one of the main plugin features. All you need to do is to provide s parameter containing the search query into the Network_Query class. Below you can find the code and the page template file.

<?php
/*
 * Template name: Multisite Search
 */

// you may want or may not want to use the header and footer of your theme
// get_header();

// getting the search query from the URL
$search_query = ! empty( $_GET[ 'searching_for' ] ) ? $_GET[ 'searching_for' ] : '';

$query = new Network_Query( array(
	'posts_per_page' => 10,
	'post_status' => 'publish',
	's' => $search_query,
) );

if( $query->have_posts() ) :

	while( $query->have_posts() ) : $query->the_post();

		switch_to_blog( $query->post->BLOG_ID );

		?><h2><a href="<?php echo get_permalink( $query->post->ID ) ?>"><?php echo get_the_title( $query->post->ID ) ?></a></h2><?php

		restore_current_blog();

	endwhile;

else :
	?><p>Nothing found</p><?php
endif;

// you may want or may not want to use the header and footer of your theme
// get_footer();

A stringsearching_for here is a name of a URL parameter you can change for your needs. You may have some issues with the default s though, aside from that – use whatever you like.

Download multisite-search.php

4. Working with custom fields

Displaying custom field values within the loop is no different than using standard loop functions like get_permalink() or the_title(). You just need to use get_post_meta() and that’s pretty much it.

But anyway let me break it down for you into a step by step.

First of all we need to add custom fields to our posts, right? I personally do not use ACF (Advanced Custom Fields) on my projects, but I know many of you guys do, so here I am creating an ACF field in the settings:

Simple ACF field example

Now let’s just display this field under the title. If you’re using ACF, there are three functions you can use to display a custom field value:

I am going to show you how to use all of them:

<?php
/*
 * Template name: Multisite Latest Posts with a Custom Field
 */

// you may want or may not want to use the header and footer of your theme
// get_header();

$query = new Network_Query( array(
	'posts_per_page' => 10,
	'post_status' => 'publish',
) );

if( $query->have_posts() ) :

	while( $query->have_posts() ) : $query->the_post();

		switch_to_blog( $query->post->BLOG_ID );

		?>
			<h2>
				<a href="<?php echo get_permalink( $query->post->ID ) ?>"><?php echo get_the_title( $query->post->ID ) ?></a>
			</h2>
			<p><?php
				echo get_post_meta( $query->post->ID, 'subtitle', true );
				// echo get_field( 'subtitle', $query->post->ID );
				// the_field( 'subtitle', $query->post->ID );
			?></p>
		<?php

		restore_current_blog();

	endwhile;

else :
	?><p>Nothing found</p><?php
endif;

// you may want or may not want to use the header and footer of your theme
// get_footer();

Download multisite-latest-posts-with-custom-fields.php

5. Display WooCommerce products from specific blogs

The last but not the least let’s take a look at a WooCommerce example where I am about to show you not only how to display products from different multisite stores at the same time but also how to provide specific multisite sites into the Network_Query.

<?php
/*
 * Template name: Multisite WooCommerce Products
 */

// you may want or may not want to use the header and footer of your theme
// get_header();

$query = new Network_Query( array(
	'posts_per_page' => 10,
	'post_status' => 'publish',
	'post_type' => 'product',
	'blog_id' => array( 2, 3 ), // for example we need to get products only from Sites 2 and 3
) );

if( $query->have_posts() ) :

	while( $query->have_posts() ) : $query->the_post();

		switch_to_blog( $query->post->BLOG_ID );

		$product = wc_get_product( $query->post->ID );

		?>
			<h2>
				<a href="<?php echo $product->get_permalink() ?>"><?php echo $product->get_title() ?></a>
			</h2>
			<?php echo $product->get_price_html() ?>
		<?php

		restore_current_blog();

	endwhile;

else :
	?><p>No products found</p><?php
endif;

// you may want or may not want to use the header and footer of your theme
// get_footer();

Just a couple things to keep in mind:

Download multisite-woocommerce-products.php

Interested in displaying WooCommerce orders from different sub-sites? Please contact me.

Need more help?