I have a multisite and I’m able to use the rest api for posts/tags/etc for specific sites but I was wondering if there’s a away to get a list of sites on the network using the rest api?
something like this: REST API for Multisite but without a plugin?
I have a multisite and I’m able to use the rest api for posts/tags/etc for specific sites but I was wondering if there’s a away to get a list of sites on the network using the rest api?
something like this: REST API for Multisite but without a plugin?
Share Improve this question asked Aug 20, 2018 at 15:23 NandoNando 112 bronze badges2 Answers
Reset to default 2There is no built-in endpoint for sites on a multisite network. You can easily create a new custom route for your api adding it to your functions.php:
function my_api_custom_route_sites() {
$args = array(
'public' => 1, // I only want the sites marked Public
'archived' => 0,
'mature' => 0,
'spam' => 0,
'deleted' => 0,
);
$sites = wp_get_sites( $args );
return $sites;
}
add_action('rest_api_init', function() {
register_rest_route('wp/v2', 'sites', [
'methods' => 'GET',
'callback' => 'my_api_custom_route_sites'
]);
});
This will output all sites in your network if you access: "/wp-json/wp/v2/sites". You can create any custom endpoint without any plugin.
Hope it helps!
There is no built-in endpoint for sites on a multisite network. As can be seen in the documentation, the built-in endpoints are:
+----------------+-------------------+
| Posts | /wp/v2/posts |
+----------------+-------------------+
| Post Revisions | /wp/v2/revisions |
+----------------+-------------------+
| Categories | /wp/v2/categories |
+----------------+-------------------+
| Tags | /wp/v2/tags |
+----------------+-------------------+
| Pages | /wp/v2/pages |
+----------------+-------------------+
| Comments | /wp/v2/comments |
+----------------+-------------------+
| Taxonomies | /wp/v2/taxonomies |
+----------------+-------------------+
| Media | /wp/v2/media |
+----------------+-------------------+
| Users | /wp/v2/users |
+----------------+-------------------+
| Post Types | /wp/v2/types |
+----------------+-------------------+
| Post Statuses | /wp/v2/statuses |
+----------------+-------------------+
| Settings | /wp/v2/settings |
+----------------+-------------------+
Accessing sites via a REST API endpoint will require a plugin. Either one you'd write yourself, or one that's already available.