A junior dev on my team—let’s call him Sam—was working on a WooCommerce site where a custom “Staff Picks” page was timing out. He pushed a fix, and sure enough, the page loaded. When I asked him to walk me through the change in the pull request, he got a little fuzzy. He said, “Well, I found this snippet that loops through the picks and checks if they are in stock.”
I looked at the code. He was running a new WP_Query inside the main WordPress loop. A total N+1 query bomb. It “worked” for the 10 picks on his local, but it was a nightmare waiting to happen on production with thousands of products. He knew how to copy and paste, but he didn’t understand why it was a bad idea. And here’s the kicker: I remember being Sam.
Early in my career, my solutions were often held together with duct tape and Stack Overflow snippets. I could make things work, but I couldn’t always explain the theory. It wasn’t until I had to start mentoring other developers that my own understanding was forced to level up. It’s a concept I was reminded of in an article over at carlalexander.ca, this idea of learning by teaching.
You Don’t Understand It Until You Can Explain It
There’s an old saying, often misattributed to Einstein: “If you can’t explain it simply, you don’t understand it well enough.” This is the entire job for a senior dev. Clients and junior devs don’t need a code dump; they need a simple explanation. Teaching forces you to find that simplicity.
My first attempt at fixing that N+1 query bomb years ago would have been to just throw a transient on it. Cache the result. And yeah, that would probably “work” for a bit. But it’s a patch, not a solution. You’re just hiding the inefficient code instead of fixing it. The real fix, the one I had to explain to Sam, is to work with WordPress, not against it.
Instead of running a second query on the page, you modify the main one before it even runs. For his staff picks page, the right tool for the job is almost always the pre_get_posts hook.
add_action( 'pre_get_posts', 'ahmad_filter_staff_picks_archive' );
function ahmad_filter_staff_picks_archive( $query ) {
// Only run on the main query for our specific post type archive.
if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'product' ) && is_page('staff-picks') ) {
$query->set( 'meta_key', '_is_staff_pick' );
$query->set( 'meta_value', 'yes' );
}
}Explaining this to him was the key. I had to articulate why this is better. It’s one database query instead of potentially dozens. It respects the WordPress query lifecycle. It’s clean. By teaching him, I reinforced the core concept for myself. He learned a fundamental WordPress pattern, and I became a better developer for it.
So, What’s the Point?
Getting something to work is just the first step. That’s baseline. To really grow, you have to be able to articulate the why. The fastest way to get there? Teach someone. It could be a junior dev, a client, or even just writing a blog post. The act of translating a complex idea into simple terms forces a level of mastery that “just doing it” never will. Trust me on this.
Look, this stuff gets complicated fast. If you’re tired of debugging someone else’s mess and just want your site to work, drop my team a line. We’ve probably seen it before.
Leave a Reply