Retrieving WordPress Sites using WP_Site_Query

WordPress 4.5 introduced to the world the WP_Site class. WordPress 4.6 is going to help you query sites in your network using the new WP_Site_Query class.

 

What’s a site, really?

WordPress has a neat feature called “WordPress Multisite” which allows you to create multiple sites with just a single installation. With multisite enabled, you can create multiple independent sites that are not connected to each other by any means, or, create a “network” of sites which are directly related to each other. If you really want to take WordPress to the extreme, you can create a network of networks, which is a multi-level network containing other networks and each of those has it’s own sites. 🙂

A “site” is a single instance in the wp_blogs DB table. A “site” can simply be the website created by WordPress, or a virtual website created as part of a network by the multisite feature.

 

Querying Sites

As mentioned above, WordPress 4.5 introduced the WP_Site class which introduces interactivity options with a site on a multisite network. The class can be used for retrieving site data as well as for setting it up.

As of WordPress 4.6, we can use the WP_Site_Query class for querying sites from the database. This neat query class returns an array which contains a list of sites matching the search criteria (Each array entry being an instanced object of WP_Site).

The new Query class helps query sites the same way we currently query WordPress Posts, Users, Comments, Terms and other common components. It’s basically an easy way to filter down items from the database, without having to write custom code, while leveraging WordPress’ core APIs, such as the Cache API for instance.

 

Site Query Generator

Just like we did with other Query Generators, we created a new generator to help you get started using this cool new class. Introducing our new WP_Site_Query Generator! It a free generator open to the general public.

WP_Site_Query Generator
WP_Site_Query Generator

 

Note that although our Site Query Generator simplifies the class internals, it’s an advanced tool. You should have deep multisite knowledge whenever interacting with sites on your network.

Should you have any questions, suggestions or marriage proposals — let me know in the comments section below.

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on whatsapp
Share on email
Rami Yushuvaev

Rami Yushuvaev

I'm an entrepreneur, a web developer and a blogger. I’ve contributed code to each and every release since WordPress 2.8. I'm the Hebrew GTE responsible for the translation and the release of WordPress Hebrew version. The founder GenerateWP.com and several other WordPress related projects. I work mainly with Israeli startups and hi-tech companies, providing development services.

3 Comments:

  • devinwalker

    Nice! I see this being ver useful for admins with large multisite installs.

    Reply

  • George

    Sorry for the stupid question, but I really can’t see a real use case for this option (maybe I am not so familar with multisiting, although i know what it is). Can you please exaplain one example use case?

    Reply

    • Rami Yushuvaev

      Well George, I’t like asking what use is there for the post query class (WP_Query). Those who create custom theme for clients are using custom post queries on a daily base. In the past it was hard to create custom queries, but with the introduction of WP_Query it’s much simpler.

      Same for sites query. Currently, it’s hard to query multisite sites. The new WP_Site_Query simplifying the process.

      We have examples on the generator page:

      • Search specific sites by ID.
      • Return all the sites from a specific network.
      • Return sites by domains.
      • Return only public/deleted/spam sites on the network.

      Developers who operate large scale multisite networks will love this new class.

      Reply

Leave a Reply

Subscribe to our Blog

Receive email notifications when new blog posts are published.