WordPress Multisite: How to Get all Terms in a One Loop
Not so much time ago I published a very useful plugin for WordPress Multisite. It indexes all the content across the multisite network (currently: posts, post metas — custom fields, and terms, in future versions I plan to add users indexation) and allows to get the network content as if it would be on a single site.
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.
Filters
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
network_get_term_link( $term, $taxonomy='', $blog_id=null )
- $term
- You can pass term local ID, or its slug or the whole object (result of
network_get_terms()
function). If you pass the object, you can leave the second and the third parameters empty. - $taxonomy
- Taxonomy name.
- $blog_id
- Blog ID.
$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.
hi misha,
is it possible to search all posts for strictly matching words?
At the moment, searching for example the word ‘test’ returns results containing similar words (ie. ‘testable’)
Hey Lorenzo,
The multisite search works on base of the default WordPress search. I understand that at the current moment WordPress search is far away from perfect. And when you search for “art” it could return results with “part’.
I’m sure this question was already discussed in community and you could try to find a code for your theme
functions.php
file that fix this issue.And of course I will add this feature to my to do list.
I used the code below:
But the categories displayed are not complete. There are so many other categories not listed.
I want to display ALL the categories both parent and children categories across all the sites.
Hey Emmanuel,
Are the terms from other blogs displayed? Or only the terms from the current blog?