Are You Fumbling Your WordPress Queries? Read and Bookmark This Post

I'll be honest here, until recently, while I had been building WordPress queries on a daily basis, I still really didn't know the way the WordPress query is build and the best way to modify it. Go through this article and you'll be sure to learn some new things that will help you develop better themes and plugins.

Table of Contents

I’ll be honest here, until recently, while I had been building WordPress queries on a daily basis, I still really didn’t know the way the WordPress query is build and the best way to modify it.

I guess I wasn’t alone, and Automattician guru Andrew Nacin came up with the perfect antidote at WordCamp Portland, delivering a classic talk called ‘You Don’t Know Query’.

It’s a 40 minute guide to WordPress queries that should straighten things out for you once and for all. Be warned though, it’s not very easy to understand the concepts Andrew discusses here, I had to listen through it twice and make reference to the Codex to really understand what he was saying.

Slides: http://www.slideshare.net/andrewnacin/you-dont-know-query-wordcamp-portland-2011

What I found missing in Andrew’s speech, were a few case studies and discussion about queries in simpler terms. Thankfully, I soon came across an excellent article by Bill Erickson that really broke things down into a simple guide for us to follow when dealing with WordPress queries. It’s an essential read if you want to learn how to query properly.

Here’s another similar post if you want a slightly different take on the subject:

http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

An important takeaway from all these articles:

To modify main loop

  • don’t use query_posts()
  • use pre_get_posts filter with $query->is_main_query() check
  • alternately use request filter (a little too rough so above is better)

To run secondary loop

Use new WP_Query or get_posts() which are pretty much interchangeable (latter is thin wrapper for former).

Where you already making the best use of WordPress queries? Any other tips to share? The comments are open!

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

Jean Galea
Jean Galea
Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Consider sharing this post so others can find it:

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services just like this one every week.

Featured Deals

Bluehost is WP Mayor’s recommended WordPress shared hosting provider. Get up to $15 off every month on Bluehost’s shared hosting using the button below.

Get 4 months free on annual WP Engine hosting plans (or 20% off your first month on monthly plans) using our coupon code WPMAYOR20 at checkout.

Get an exclusive 15% discount on WooFunnels by clicking the link below. It’s the most powerful funnel builder for all types of WooCommerce stores.

Leave a Reply

Your email address will not be published.

Everything you need to create a website for less than $100. From your domain and hosting to picking a design, we cover it all.

WP Mayor’s Newsletter

Team photo

Hey, we’re Gaby, Prithu, and Mark. Every Friday we send you our best tutorials and latest reviews.

Enjoyed by thousands every week. Less than 0.2% of our followers unsubscribed last year.