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:
- Post object
$query->postis available inside the loop anyway but in order to use loop functions likeget_permalink()andget_the_title()we need to switch to a specific blog first. If you have a lot of network sites to loop through (more than 10), you may not like the necessaty of switching to every sub-site every time. The good news – this can be optimised, here is the tutorial how. - As you can see we’re using
get_permalink()andget_the_title()with providing the current post ID within the loop (instead ofthe_permalink()andthe_title()without arguments). It is becauseNetwork_Querydoesn’t set$postobject automatically but you can do it like this:global $post; $post = $query->post;and then use the loop functions e.g.the_permalink(). We’re going to take a look at this in the next example.
Page template file:
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:
- You can read about creating custom paginations in a separate tutorial.
- I hardcoded the search page URL on the line 40, do not forget to replace it with your search page URL or you can also provide it dynamically.
current_pageis a URL parameter you can change for your needs.
The page template file is below:
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.
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:

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:
get_post_meta()– I already mentioned it before,get_field(),the_field().
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();
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:
- WooCommerce should be activated on the site we’re going to use this template, otherwise
wc_get_product()function will not be available for us (you will get a fatal error probably). - Don’t forget to allow
productpost type for indexing. Both in plugin global settings and in site-specific settings.
Interested in displaying WooCommerce orders from different sub-sites? Please contact me.