Glossary entry

WP_Query

WP_Query is the foundational PHP class WordPress uses to retrieve posts, pages, and custom post type entries from the database. Every page load on a WordPress site runs at least one WP_Query: the "main query" that fetches the content the current URL is supposed to display. Developers use WP_Query directly when they need a secondary loop — a block or widget that pulls a different set of posts than the current page. A query is constructed by instantiating the class with an array of arguments: post_type (which CPT to query), posts_per_page, meta_query (filter by post meta values), tax_query (filter by taxonomy terms), orderby, and date_query. The resulting object exposes have_posts() and the_post() methods for the standard template loop. The pre_get_posts hook is the correct way to modify the main query before it runs — never instantiate a new WP_Query just to change the main loop.

No published articles use WP_Query yet.

When new articles use this term, they will appear here.