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.
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.
3 Comments:
devinwalker
Nice! I see this being ver useful for admins with large multisite installs.
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?
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 ofWP_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:
Developers who operate large scale multisite networks will love this new class.