WordPress Multisite: How to Get all Terms in a One Loop

Introduction into network_get_terms() — the function which allows you to get all the terms across the WordPress Multisite Network in a one foreach loop.

#WordPress Multisite, #taxonomies  /   12

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.

All the terms across WordPress Multisite network in the database table (phpMyAdmin).

Download Plugin


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 )


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.

Taxonomy name or an array of taxonomies, e.g. array('post_tag', 'category').
An array of additional arguments.

The number of terms to get.
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.

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.

Returns the terms from the whole network whose slugs match this value. You can also specify the array of slugs.
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.
Function will search the given string in term names and slugs all over the network.
How to order the results, count — by the number of posts in terms, name (default), slug, description — by term description, include — order as in include parameter, id — by local term ID value.
ASC ascending (default) or DESC descending.
How to return the results:

  • all — array of objects (default),
  • names — array of term names,
  • ids — array of local IDs.


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->blog_id - ID of the blog, the tag belong to
$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 )
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 name.
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 . '</a></li>';
	echo '</ul>';

If you have a question, please leave it in comments.

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 12

  • can we get the post by tearm name from whole network ?

  • Hi Misha,

    Can I get post by blog id and category slug?

    $args = array(
    	'post_type'		=> 'post',
    	'blog_id' => array( $blog_selected ),
    	//'cat' => array( $id_cat ),
    	'taxonomy' => $category_slug,
    	'posts_per_page'	=> $number,
    	'showposts' => $number,
  • Hi,

    Thanks for the plugin. I’m trying to query terms from every site with specific taxonomy and in current language (Polylang):

    $network_categories = network_get_terms( array(
      'network_taxonomies'    => 'my_taxonomy',
      'hide_empty'            => false,
      'lang' => function_exists( 'pll_current_language' ) ? pll_current_language() : '',
    ) );
    if( $network_categories ){
      echo '<ul>';
      foreach ( $network_categories as $network_category ){
        echo '<li><a href="' . network_get_term_link( $network_category ) . '">' . $network_category->name . '</a></li>';
      echo '</ul>';

    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.

  • Hi Misha,

    in 4.8 update I have a problem with a function

    $network_cats = network_get_terms('category', 'name__like='.$category_slug);

    The function work but write this text in page

    SELECT t.*, tt.* FROM wp_network_terms AS t INNER JOIN wp_network_term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN (‘category’) AND t.name LIKE …


Leave your question or feedback

Please, enter a comment
Please, enter a name
Incorrect email