WordPress Multisite: How to Get all Terms in a One Loop
In this tutorial I am going to talk about how to print all the terms from your multisite network using just one query. It means that we are not going to use swith_to_blog()
solution, because there is some disadvantages to it.
- When you print categories or any other custom taxonomies with the help of
switch_to_blog()
andget_terms()
it will be tough to sort them in a desired way, and usually terms are going to be displayed separated by a site (all terms from Site 1, then all terms from Site 2 etc). - There are going to be a lot of SQL queries – for each loop iteration at least.
That’s why when you’re working with the content from all over the network is is much more efficient to create an index in a separate database tables and then to work with that index. In order to create that index you may use my plugin, in order to get terms from that index – just use network_get_terms()
functions which is also the part of the plugin.
network_get_terms()
This multisite plugin has become more popular than I expected, so I decided to make it even better. One of the first improvements I made was the function similar to get_terms()
— network_get_terms()
.
The main difference between these two functions is that get_terms()
works only within a single site and network_get_terms()
in the entire multisite network.
network_get_terms( $network_taxonomies, $network_args )
Parameters
Most parameters of the functions get_terms()
and network_get_terms()
are the same, but not all. So, I think I should describe them in details.
- $network_taxonomies
- Taxonomy name or an array of taxonomies, e.g.
array('post_tag', 'category')
. - $network_args
- An array of additional arguments.
- number
- The number of terms to get.
- offset
- The number of elements you want to offset from the beginning of the query. Works only if
number
parameter is also passed. - include, exclude
- These parameters accept only local term ID or an array of IDs.
// in this examplle we exclude the categories with ID = 1 or ID = 2 on each network sites $network_category = network_get_terms('category', array('exclude' => array( 1, 2 ) ) );
If you pass both of these parameters,
exclude
will be ignored. - parent
- Local term ID whose direct children you want the function to return.
It works for all network blogs. For example if you set the value of this parameter equal to 11, and you have the category with ID = 11 on the blog 1 and on the blog 2 too, then the function returns the direct children of these both categories.
If
0
is passed, function returns only top-level terms with no children. - $slug
- Returns the terms from the whole network whose slugs match this value. You can also specify the array of slugs.
- $name
- Returns the terms from the whole network whose names match this value. The array of names is also supported.
- $name__like, $description__like
- Specify the part of the name or the part of the description (case-insensitive). It uses
LIKE '%string%'
query in database agains term names or descriptions. - $search
- Function will search the given string in term names and slugs all over the network.
- orderby
- How to order the results,
count
— by the number of posts in terms,name
(default),slug
,description
— by term description,include
— order as ininclude
parameter,id
— by local term ID value. - order
ASC
ascending (default) orDESC
descending.- fields
- How to return the results:
all
— array of objects (default),names
— array of term names,ids
— array of local IDs.
Hooks
The network_get_terms()
has the same filters, as get_terms
, but with network_
prefix.
get_terms() | network_get_terms() |
---|---|
get_terms_args | network_get_terms_args |
get_terms | network_get_terms |
get_terms_orderby | network_get_terms_orderby |
list_terms_exclusions | network_list_terms_exclusions |
get_terms_fields | network_get_terms_fields |
terms_clauses | network_terms_clauses |
I don’t want to spend time describing each of these filters — you can find and learn them in the plugin code or just ask me in comments.
And now let’s look at the examples
The following code will work only if my plugin is installed on your network.
1. Get Post Tags from all Blogs in WP Multisite
$network_tags = network_get_terms( 'post_tag', 'orderby=name&hide_empty=0' );
if( $network_tags ){
echo '<select>';
foreach ( $network_tags as $network_tag ){
echo "<option value='{$network_tag->term_id}'>{$network_tag->name}</option>";
}
echo '</select>';
}
/*
in this example is obvious that
$network_tag->name - tag name,
$network_tag->term_id - tag global unique ID across the network
the other parameters:
$network_tag->term_local_id - the local tag ID inside the blog, which the tag belong to
$network_tag->parent - local ID of tag parent
$network_tag->taxonomy - taxonomy name, in this case: post_tag
$network_tag->slug
$network_tag->blog_id - ID of the blog, the tag belong to
$network_tag->description
$network_tag->count - the number of tagged posts
*/
You may notice that this example doesn’t describe one important thing. So, we have tag names, tag ids, but we dont’t have tag links.
Don’t worry, for these purposes I created a function — network_get_term_link()
, which is similar to default get_term_link()
, but works with WordPress Multisite.
2. Get all Category Links across the WordPress Multisite Network
To get category links you can use network_get_term_link()
function.
$network_categories = network_get_terms( 'category' );
if( $network_categories ){
echo '<ul>';
foreach ( $network_categories as $network_category ){
echo '<li><a href="' . network_get_term_link( $network_category ) . '">' . $network_category->name . '</li>';
}
echo '</ul>';
}
If you have a question, please leave it in comments.

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
can we get the post by tearm name from whole network ?
Hi,
yes, using
term_query
parameter forNetwork_Query
. More info.we have to pass the category ID and the category ID can be different for each blog, but the name can be same so is there a method we can use that in this plugin?
Yes, it is better to use category name or slug.
Hi Misha,
Can I get post by blog id and category slug?
Hi Luigi,
Yes, but I think it is better use
tax_query
.Hi,
Thanks for the plugin. I’m trying to query terms from every site with specific taxonomy and in current language (Polylang):
Currently it lists all the terms with all languages, not in currently selected language. Links also point to /sitename/blog/slug/term-slug Why does it add “blog” in there?
Also query doesn’t work at all when I’m on subsite. Only on main site.
Hi,
network_get_terms()
function doesn’t support Polylang plugin at this moment. But I plan to add it.At this moment only post queries support Polylang plugin – more info.