最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Rest API Multisite Site List?

programmeradmin8浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 2

There 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论