This plugin tests two different approaches for getting an array of posts that are grouped by each term. This plugin is a companion to the Know the Code Hands-on Advanced Lab, Get Posts Grouped by Terms. In this lab, you will learn how to use this plugin as well as a deep dive into more advanced SQL Queries and $wpdb
(i.e. WordPress' MySQL Wrapper).
- Grab the terms first using
get_terms()
and then loop through each term and query the database to grab the associated posts. - Build a more advanced SQL Query that performs the task in one database hit.
This first approach is found when doing a Google search. It involves fetching all of the terms first. Then iterating through those terms one-by-one. During the loop, this approach uses WP_Query
to get the posts that are associated with each term.
This approach works fine; however, it's less performant. Why? It requires additional database hits. Remember, the database is a bottleneck on the server side. You want to optimize your trips to the database.
The second approach uses $wpdb
with a custom SQL query. This query joins the multiple tables together and then groups them. It allows us to fetch the records in one database hit....just ONE trip to the database. That's much faster.
We use this approach on Know the Code, as part of our Help Center.
For this lab, you will need to install the following plugins. You should have both of these installed on all of your projects. These two plugins are for development only and not to be deployed to a live production site.
- UpDevTools - A suite of Developer tools including Kint, Whoops, and more.
- Query Monitor - We use Query Monitor to analyze the queries.
You will need a sandbox.dev site spun up on your local machine using DesktopServer, VVV, or your favorite web appliance. Any theme will do.
Make sure that you have a sandbox site setup. We have a Sandbox repo ready for you. It has everything you need, including dummy content. Use this sandbox to work along with Tonya.
- In terminal, navigate to
{path to your sandbox project}/wp-content/plugins
. - Then type in terminal:
git clone https://github.com/KnowTheCode/groupby-lab-tester.git
- Log into your Sandbox site.
- Go to Plugins and activate the plugin.