How to Get all Terms in a One Loop for multisite
Table of Contents
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.
$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 in include parameter, id — by local term ID value.
order
ASC ascending (default) or DESC 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
1. Get Post Tags from all Blogs in WP Multisite
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.
$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 */
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.