{"id":4318,"date":"2025-02-05T18:20:08","date_gmt":"2025-02-06T00:20:08","guid":{"rendered":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/"},"modified":"2026-03-23T22:12:33","modified_gmt":"2026-03-24T03:12:33","slug":"wp_query-in-wordpress","status":"publish","type":"post","link":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/","title":{"rendered":"Perform Queries: WordPress WP_Query"},"content":{"rendered":"<p data-analytics-track-visibility=\"yes\"><a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/codex.wordpress.org\/Database_Description\" target=\"_blank\" rel=\"noreferrer noopener\">The database<\/a> that feeds your WordPress website is full of valuable information. This is what makes it possible to filter your posts and pages by many different variables. If what you want to display is not part of your theme, however, there are other ways of using that data.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">This is where <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/\" target=\"_blank\" rel=\"noreferrer noopener\">WP_Query<\/a> comes in. This is a <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/www.php.net\/manual\/en\/language.oop5.php\" target=\"_blank\" rel=\"noreferrer noopener\">PHP class<\/a> that makes use of a wide variety of parameters. Consequently, it enables you to pull data from the WordPress database for use or display on your website.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">In this article, we\u2019ll provide a deeper understanding of the WP_Query class and how it can be used. We\u2019ll also walk you through some important steps to follow when using it. If you\u2019re ready, let\u2019s dive right in!&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-what-is-wp_query\" data-analytics-track-visibility=\"yes\">What is WP_Query?<\/h2>\n\n\n\n<p data-analytics-track-visibility=\"yes\">As we mentioned, WP_Query is a PHP class used by the WordPress database. This particular class can do several things, but primarily it\u2019s used to pull posts from the database.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">As its name indicates, it makes a query based on the criteria you set for it. Since there are <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#parameters\" target=\"_blank\" rel=\"noreferrer noopener\">a lot of parameters<\/a> you can use with WP_Query in WordPress, you can pull and display posts in a number of unique ways. We\u2019ll explore those options in greater detail later in this post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-how-to-use-wp_query\" data-analytics-track-visibility=\"yes\">How to Use WP_Query<\/h2>\n\n\n\n<p data-analytics-track-visibility=\"yes\">Even if you\u2019re just learning the various aspects of the WordPress codebase, WP_Query is a good class to get started with. Now, let\u2019s break down four different ways you can use it on your website.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-1-get-started-with-a-custom-loop\" data-analytics-track-visibility=\"yes\">1. Get Started with a Custom Loop&nbsp;<\/h3>\n\n\n\n<p data-analytics-track-visibility=\"yes\">One of the best ways to get to know the WP_Query call is through <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/themes\/basics\/the-loop\/\" target=\"_blank\" rel=\"noreferrer noopener\">the WordPress Loop<\/a>. If you\u2019re not familiar with what the Loop is, it\u2019s an important concept to read up on.<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">The Loop is what calls to the database asking for post content, and displays the data that is returned. It also functions based on set parameters, such as how many posts you want your site to display on a single page (something you can configure in your <em>Settings &gt; Reading<\/em> menu).<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">The very basics of the Loop look like this:<\/p>\n\n\n<div class=\"wp-block-code-wrapper wp-block-acf-code\" data-analytics-track-visibility=\"yes\">\n\t<pre class=\"wp-block-code\"><code>&lt;?php\nif ( have_posts() ) :\n&nbsp;&nbsp;&nbsp;&nbsp;while ( have_posts() ) : the_post();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Display post content\n&nbsp;&nbsp;&nbsp;&nbsp;endwhile;\nendif;\n?&gt;<\/code><\/pre>\n\t<div class=\"wp-block-code-controls\">\n\t\t<button class=\"wp-block-code-button\" aria-label=\"Copy code to clipboard\" type=\"button\"><svg viewBox=\"0 0 24 24\" fill=\"none\" aria-hidden=\"true\"><path d=\"M5 2C3.34315 2 2 3.34315 2 5V16C2 16.5523 2.44772 17 3 17C3.55228 17 4 16.5523 4 16V5C4 4.44772 4.44772 4 5 4H16C16.5523 4 17 3.55228 17 3C17 2.44772 16.5523 2 16 2H5Z\" fill=\"currentColor\" \/><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M9 6C7.34315 6 6 7.34315 6 9V19C6 20.6569 7.34315 22 9 22H19C20.6569 22 22 20.6569 22 19V9C22 7.34315 20.6569 6 19 6H9ZM8 9C8 8.44772 8.44772 8 9 8H19C19.5523 8 20 8.44772 20 9V19C20 19.5523 19.5523 20 19 20H9C8.44772 20 8 19.5523 8 19V9Z\" fill=\"currentColor\" \/><\/svg><\/button>\n\t\t<span class=\"toast\" role=\"status\" aria-live=\"assertive\"><\/span>\n\t<\/div>\n<\/div>\n\n\n<p data-analytics-track-visibility=\"yes\">This simple statement is essentially saying that if there are posts, they should be displayed. Of course, you can add a wide variety of <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/themes\/references\/list-of-template-tags\/\" target=\"_blank\" rel=\"noreferrer noopener\">template tags<\/a> to this foundation, in order to create the display you want.<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">You can also insert WP_Query into the Loop. This enables you to place parameters on what posts will be returned. Let\u2019s break down what that would look like:&nbsp;<\/p>\n\n\n<div class=\"wp-block-code-wrapper wp-block-acf-code\" data-analytics-track-visibility=\"yes\">\n\t<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/\/ The Query\n$the_query = new WP_Query( $args );\n\n\/\/ The Loop\nif ( $the_query-&gt;have_posts() ) {\n&nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;ul&gt;';\n&nbsp;&nbsp;&nbsp;&nbsp;while ( $the_query-&gt;have_posts() ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$the_query-&gt;the_post();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;li&gt;' . get_the_title() . '&lt;\/li&gt;';\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;echo '&lt;\/ul&gt;';\n} else {\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/ no posts found\n}\n\/* Restore original Post Data *\/\nwp_reset_postdata();<\/code><\/pre>\n\t<div class=\"wp-block-code-controls\">\n\t\t<button class=\"wp-block-code-button\" aria-label=\"Copy code to clipboard\" type=\"button\"><svg viewBox=\"0 0 24 24\" fill=\"none\" aria-hidden=\"true\"><path d=\"M5 2C3.34315 2 2 3.34315 2 5V16C2 16.5523 2.44772 17 3 17C3.55228 17 4 16.5523 4 16V5C4 4.44772 4.44772 4 5 4H16C16.5523 4 17 3.55228 17 3C17 2.44772 16.5523 2 16 2H5Z\" fill=\"currentColor\" \/><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M9 6C7.34315 6 6 7.34315 6 9V19C6 20.6569 7.34315 22 9 22H19C20.6569 22 22 20.6569 22 19V9C22 7.34315 20.6569 6 19 6H9ZM8 9C8 8.44772 8.44772 8 9 8H19C19.5523 8 20 8.44772 20 9V19C20 19.5523 19.5523 20 19 20H9C8.44772 20 8 19.5523 8 19V9Z\" fill=\"currentColor\" \/><\/svg><\/button>\n\t\t<span class=\"toast\" role=\"status\" aria-live=\"assertive\"><\/span>\n\t<\/div>\n<\/div>\n\n\n<p data-analytics-track-visibility=\"yes\">You\u2019ll see the same <em>if\/while<\/em> statements from the basic Loop, but there is an additional WP_Query string. Whatever parameters are set here will determine what posts will be displayed.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">For example, if you wanted to <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#author-parameters\" target=\"_blank\" rel=\"noreferrer noopener\">exclude a certain author<\/a> from a list of posts, you could do that in the Loop with WP_Query:&nbsp;<\/p>\n\n\n<div class=\"wp-block-code-wrapper wp-block-acf-code\" data-analytics-track-visibility=\"yes\">\n\t<pre class=\"wp-block-code\"><code>$query = new WP_Query( array( 'author' =&gt; -12 ) );<\/code><\/pre>\n\t<div class=\"wp-block-code-controls\">\n\t\t<button class=\"wp-block-code-button\" aria-label=\"Copy code to clipboard\" type=\"button\"><svg viewBox=\"0 0 24 24\" fill=\"none\" aria-hidden=\"true\"><path d=\"M5 2C3.34315 2 2 3.34315 2 5V16C2 16.5523 2.44772 17 3 17C3.55228 17 4 16.5523 4 16V5C4 4.44772 4.44772 4 5 4H16C16.5523 4 17 3.55228 17 3C17 2.44772 16.5523 2 16 2H5Z\" fill=\"currentColor\" \/><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M9 6C7.34315 6 6 7.34315 6 9V19C6 20.6569 7.34315 22 9 22H19C20.6569 22 22 20.6569 22 19V9C22 7.34315 20.6569 6 19 6H9ZM8 9C8 8.44772 8.44772 8 9 8H19C19.5523 8 20 8.44772 20 9V19C20 19.5523 19.5523 20 19 20H9C8.44772 20 8 19.5523 8 19V9Z\" fill=\"currentColor\" \/><\/svg><\/button>\n\t\t<span class=\"toast\" role=\"status\" aria-live=\"assertive\"><\/span>\n\t<\/div>\n<\/div>\n\n\n<p data-analytics-track-visibility=\"yes\">By placing this in the Loop, your displayed posts would no longer include the user with the author number of 12.<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">The <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#properties-and-methods\" target=\"_blank\" rel=\"noreferrer noopener\">number of parameters<\/a> you can use with this method are just about endless. You can <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#category-parameters\" target=\"_blank\" rel=\"noreferrer noopener\">include category information<\/a> as well as <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#taxonomy-parameters\" target=\"_blank\" rel=\"noreferrer noopener\">advanced taxonomies<\/a>, just to give two examples.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-2-arguments-the-backbone-of-custom-queries-in-wordpress\" data-analytics-track-visibility=\"yes\">2. Arguments: The Backbone of Custom Queries in WordPress<\/h3>\n\n\n\n<p data-analytics-track-visibility=\"yes\">In the previous example, you may have noticed \u201c($args)\u201d as a part of the string. This is a vital part of the query that refers to the included \u201carguments.\u201d It tells the database exactly what to include in the returned data.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">Essentially, these arguments can be set up to determine the exact results you want to display. Arguments can be used to change the value of variables as well. For example, if you want to change how your list of categories appears on the page, you can use an argument.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">Your argument will define an array of variables and values. So if you want to, you can use an argument to define an array and tell your database to present the categories in descending order. Additionally, you can use the same method to exclude any categories that don\u2019t contain posts.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-3-parameters-in-wp_query-category-tag-and-more\" data-analytics-track-visibility=\"yes\">3. Parameters in WP_Query: Category, Tag, and More&nbsp;<\/h3>\n\n\n\n<p data-analytics-track-visibility=\"yes\">Up to this point, we\u2019ve only mentioned parameters in passing. At this point, let\u2019s look closer at what they can actually do. Their primary function is to enable you to pull custom-designed collections of posts.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">One <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#parameters\" target=\"_blank\" rel=\"noreferrer noopener\">example of a parameter<\/a> that can be used in the header of your site is the <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#category-parameters\" target=\"_blank\" rel=\"noreferrer noopener\">Category parameter<\/a>. You can use this to specify specific categories for display. This is done by providing the relevant category number or slug.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">Additionally, you can do the same thing with the <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#tag-parameters\" target=\"_blank\" rel=\"noreferrer noopener\">Tag parameter<\/a>. Of course, the Category and Tag parameters are really just the tip of the iceberg when it comes to using WP_Query.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-4-modify-objects-with-methods-and-properties\" data-analytics-track-visibility=\"yes\">4. Modify Objects with Methods and Properties&nbsp;<\/h3>\n\n\n\n<p data-analytics-track-visibility=\"yes\">While it\u2019s not recommended to directly alter the <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#properties-and-methods\" target=\"_blank\" rel=\"noreferrer noopener\">properties of a class<\/a> like WP_Query, you can interact with them <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/methods\/\" target=\"_blank\" rel=\"noreferrer noopener\">by using methods<\/a>. Essentially, methods are like <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/functions\/\" target=\"_blank\" rel=\"noreferrer noopener\">functions<\/a>, while properties are the equivalent of <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/codex.wordpress.org\/Global_Variables\" target=\"_blank\" rel=\"noreferrer noopener\">variables<\/a>.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">WP_Query has <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/#properties\" target=\"_blank\" rel=\"noreferrer noopener\">many properties<\/a>. These range from simple \u201c$posts\u201d properties to more complicated ones. Whatever method is used to interact with them, data will be returned based on the parameters you choose to put in place.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-wp_query-vs-query_posts-in-wordpress\" data-analytics-track-visibility=\"yes\">WP_Query vs. query_posts() in WordPress<\/h2>\n\n\n\n<p data-analytics-track-visibility=\"yes\">It\u2019s worth noting there is another way to modify the main query on your page. This is the query_posts() function. While this can work in a similar way to WP_Query, it can also be very problematic.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\"><a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/functions\/query_posts\/\" target=\"_blank\" rel=\"noreferrer noopener\">The WordPress Code Reference<\/a> even strongly advises that you do not use this function inside your website\u2019s main Loop. It\u2019s also best to avoid it in plugins and themes. This is because it will completely override your main query.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">The WP_Query class is preferred, because you can also \u201creset\u201d the main Loop after you run a query. Since WP_Query allows you to run multiple queries in a loop, you\u2019ll want to understand how to implement the <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_reset_postdata\/\" target=\"_blank\" rel=\"noreferrer noopener\">wp_reset_postdata<\/a> function as well.<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">If you\u2019ve embedded a secondary loop inside the main WordPress Loop, the reset function will be placed at the end and look like this:&nbsp;<\/p>\n\n\n<div class=\"wp-block-code-wrapper wp-block-acf-code\" data-analytics-track-visibility=\"yes\">\n\t<pre class=\"wp-block-code\"><code>&lt;?php wp_reset_postdata(); ?&gt;<\/code><\/pre>\n\t<div class=\"wp-block-code-controls\">\n\t\t<button class=\"wp-block-code-button\" aria-label=\"Copy code to clipboard\" type=\"button\"><svg viewBox=\"0 0 24 24\" fill=\"none\" aria-hidden=\"true\"><path d=\"M5 2C3.34315 2 2 3.34315 2 5V16C2 16.5523 2.44772 17 3 17C3.55228 17 4 16.5523 4 16V5C4 4.44772 4.44772 4 5 4H16C16.5523 4 17 3.55228 17 3C17 2.44772 16.5523 2 16 2H5Z\" fill=\"currentColor\" \/><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M9 6C7.34315 6 6 7.34315 6 9V19C6 20.6569 7.34315 22 9 22H19C20.6569 22 22 20.6569 22 19V9C22 7.34315 20.6569 6 19 6H9ZM8 9C8 8.44772 8.44772 8 9 8H19C19.5523 8 20 8.44772 20 9V19C20 19.5523 19.5523 20 19 20H9C8.44772 20 8 19.5523 8 19V9Z\" fill=\"currentColor\" \/><\/svg><\/button>\n\t\t<span class=\"toast\" role=\"status\" aria-live=\"assertive\"><\/span>\n\t<\/div>\n<\/div>\n\n\n<p data-analytics-track-visibility=\"yes\">This will restore the default template tags, and you\u2019ll be back to how things were before your secondary loop was initiated.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-customize-your-wordpress-site-with-wp-engine\" data-analytics-track-visibility=\"yes\">Customize Your WordPress Site with WP Engine&nbsp;<\/h2>\n\n\n\n<p data-analytics-track-visibility=\"yes\">Being able to make adjustments to how items are displayed on your website is just one of the benefits of using WordPress. Understanding WP_Query and leveraging the tools provided on the <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"outbound\" href=\"https:\/\/developer.wordpress.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">developer resources<\/a> page can help you build truly customized websites.&nbsp;<\/p>\n\n\n\n<p data-analytics-track-visibility=\"yes\">Here at WP Engine, we\u2019re passionate about making sure you have the resources and high-quality <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"internal\" href=\"https:\/\/wpengine.com\/wordpress-hosting\/\" target=\"_blank\" rel=\"noreferrer noopener\">hosting for WordPress sites<\/a> so you can focus on building and managing engaging websites. Check out our <a data-analytics-action-type=\"link\" data-analytics-link-location=\"Post Content\" data-analytics-link-type=\"internal\" href=\"https:\/\/wpengine.com\/plans\/\" target=\"_blank\" rel=\"noreferrer noopener\">solutions and hosting plans for WordPress sites<\/a> today!&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>The database that feeds your WordPress website is full of valuable information. This is what makes it possible to filter your posts and pages by many different variables. If what you want to display is not part of your theme, however, there are other ways of using that data.&nbsp; This is where WP_Query comes in. [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":5482,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":true,"mediapress_draft_name":"","_mediapress_is_draft_copy":false,"_time_to_read":7,"footnotes":""},"audience":[],"blog-category":[121],"buyer-stage":[],"company-and-culture":[],"content-type":[43],"location":[],"persona":[106],"product":[],"topic":[95],"use-cases":[],"class_list":["post-4318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","blog-category-best-practices","content-type-article","persona-developer","topic-development"],"acf":{"hero_image_alt_text":"","featured_on_hub_page":false,"featured_on_tag_page":false,"featured_on_category_page":false,"wp_engine_pick":false,"display_author_bio":false,"taxonomy_selector":{"":null,"taxonomy-audience":false,"taxonomy-buyer-stage":false,"taxonomy-company-and-culture":false,"taxonomy-content-type":[43],"taxonomy-location":false,"taxonomy-persona":[106],"taxonomy-product":false,"taxonomy-topic":[95],"taxonomy-use-cases":false}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Use WP_Query in WordPress<\/title>\n<meta name=\"description\" content=\"WP_Query allows users to perform complex queries to retrieve data from the WordPress database. Learn how to use WP_Query in WordPress.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use WP_Query in WordPress\" \/>\n<meta property=\"og:description\" content=\"WP_Query allows users to perform complex queries to retrieve data from the WordPress database. Learn how to use WP_Query in WordPress with WP Engine.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\u00ae\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-06T00:20:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-24T03:12:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/wp-content\/uploads\/2025\/09\/WPE-IMG-Thumbnail-1200x630-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sam Rodriguez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"How to Use WP_Query in WordPress\" \/>\n<meta name=\"twitter:description\" content=\"WP_Query allows users to perform complex queries to retrieve data from the WordPress database. Learn how to use WP_Query in WordPress with WP Engine.\" \/>\n<meta name=\"twitter:creator\" content=\"@wpengine\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sam Rodriguez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/\"},\"author\":{\"name\":\"Sam Rodriguez\",\"@id\":\"https:\\\/\\\/wpengine.com\\\/#\\\/schema\\\/person\\\/9625524bbe6079f6f8795d393dc2c22f\"},\"headline\":\"Perform Queries: WordPress WP_Query\",\"datePublished\":\"2025-02-06T00:20:08+00:00\",\"dateModified\":\"2026-03-24T03:12:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/\"},\"wordCount\":1188,\"publisher\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wpengine.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/Perform-Queries-in-Wordpress-with-WP_Query.-image-depicts-a-woman-usin-1.png\",\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/\",\"url\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/\",\"name\":\"How to Use WP_Query in WordPress\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wpengine.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/Perform-Queries-in-Wordpress-with-WP_Query.-image-depicts-a-woman-usin-1.png\",\"datePublished\":\"2025-02-06T00:20:08+00:00\",\"dateModified\":\"2026-03-24T03:12:33+00:00\",\"description\":\"WP_Query allows users to perform complex queries to retrieve data from the WordPress database. Learn how to use WP_Query in WordPress.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/#primaryimage\",\"url\":\"https:\\\/\\\/wpengine.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/Perform-Queries-in-Wordpress-with-WP_Query.-image-depicts-a-woman-usin-1.png\",\"contentUrl\":\"https:\\\/\\\/wpengine.com\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/Perform-Queries-in-Wordpress-with-WP_Query.-image-depicts-a-woman-usin-1.png\",\"width\":1200,\"height\":675,\"caption\":\"Perform Queries in Wordpress with WP_Query. image depicts a woman using a laptop\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/wp_query-in-wordpress\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/wpengine.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Perform Queries: WordPress WP_Query\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wpengine.com\\\/#website\",\"url\":\"https:\\\/\\\/wpengine.com\\\/\",\"name\":\"WP Engine\u00ae\",\"description\":\"Managed Hosting for WordPress\",\"publisher\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wpengine.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/wpengine.com\\\/#organization\",\"name\":\"WP Engine\",\"url\":\"https:\\\/\\\/wpengine.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wpengine.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/wpengine.com\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/WPEngine_OGImage-1.webp\",\"contentUrl\":\"https:\\\/\\\/wpengine.com\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/WPEngine_OGImage-1.webp\",\"width\":1200,\"height\":630,\"caption\":\"WP Engine\"},\"image\":{\"@id\":\"https:\\\/\\\/wpengine.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/wpengine\\\/\",\"https:\\\/\\\/x.com\\\/wpengine\",\"https:\\\/\\\/www.instagram.com\\\/wpengine\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/wpengine\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCJeAEAxX69v24CUBZ0WBYSg\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/wpengine.com\\\/#\\\/schema\\\/person\\\/9625524bbe6079f6f8795d393dc2c22f\",\"name\":\"Sam Rodriguez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/933722cf8761e0c08fbced6085998032df460c5ecfa2481d9cd16f569f3da2c1?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/933722cf8761e0c08fbced6085998032df460c5ecfa2481d9cd16f569f3da2c1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/933722cf8761e0c08fbced6085998032df460c5ecfa2481d9cd16f569f3da2c1?s=96&d=mm&r=g\",\"caption\":\"Sam Rodriguez\"},\"url\":\"https:\\\/\\\/wpengine.com\\\/blog\\\/author\\\/sam-rodriguez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use WP_Query in WordPress","description":"WP_Query allows users to perform complex queries to retrieve data from the WordPress database. Learn how to use WP_Query in WordPress.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/","og_locale":"en_US","og_type":"article","og_title":"How to Use WP_Query in WordPress","og_description":"WP_Query allows users to perform complex queries to retrieve data from the WordPress database. Learn how to use WP_Query in WordPress with WP Engine.","og_url":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/","og_site_name":"WP Engine\u00ae","article_publisher":"https:\/\/www.facebook.com\/wpengine\/","article_published_time":"2025-02-06T00:20:08+00:00","article_modified_time":"2026-03-24T03:12:33+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/wpengine.com\/wp-content\/uploads\/2025\/09\/WPE-IMG-Thumbnail-1200x630-1.jpg","type":"image\/jpeg"}],"author":"Sam Rodriguez","twitter_card":"summary_large_image","twitter_title":"How to Use WP_Query in WordPress","twitter_description":"WP_Query allows users to perform complex queries to retrieve data from the WordPress database. Learn how to use WP_Query in WordPress with WP Engine.","twitter_creator":"@wpengine","twitter_site":"@wpengine","twitter_misc":{"Written by":"Sam Rodriguez","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/#article","isPartOf":{"@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/"},"author":{"name":"Sam Rodriguez","@id":"https:\/\/wpengine.com\/#\/schema\/person\/9625524bbe6079f6f8795d393dc2c22f"},"headline":"Perform Queries: WordPress WP_Query","datePublished":"2025-02-06T00:20:08+00:00","dateModified":"2026-03-24T03:12:33+00:00","mainEntityOfPage":{"@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/"},"wordCount":1188,"publisher":{"@id":"https:\/\/wpengine.com\/#organization"},"image":{"@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/#primaryimage"},"thumbnailUrl":"https:\/\/wpengine.com\/wp-content\/uploads\/2025\/02\/Perform-Queries-in-Wordpress-with-WP_Query.-image-depicts-a-woman-usin-1.png","inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/","url":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/","name":"How to Use WP_Query in WordPress","isPartOf":{"@id":"https:\/\/wpengine.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/#primaryimage"},"image":{"@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/#primaryimage"},"thumbnailUrl":"https:\/\/wpengine.com\/wp-content\/uploads\/2025\/02\/Perform-Queries-in-Wordpress-with-WP_Query.-image-depicts-a-woman-usin-1.png","datePublished":"2025-02-06T00:20:08+00:00","dateModified":"2026-03-24T03:12:33+00:00","description":"WP_Query allows users to perform complex queries to retrieve data from the WordPress database. Learn how to use WP_Query in WordPress.","breadcrumb":{"@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/#primaryimage","url":"https:\/\/wpengine.com\/wp-content\/uploads\/2025\/02\/Perform-Queries-in-Wordpress-with-WP_Query.-image-depicts-a-woman-usin-1.png","contentUrl":"https:\/\/wpengine.com\/wp-content\/uploads\/2025\/02\/Perform-Queries-in-Wordpress-with-WP_Query.-image-depicts-a-woman-usin-1.png","width":1200,"height":675,"caption":"Perform Queries in Wordpress with WP_Query. image depicts a woman using a laptop"},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/blog\/wp_query-in-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/"},{"@type":"ListItem","position":2,"name":"Perform Queries: WordPress WP_Query"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/#website","url":"https:\/\/wpengine.com\/","name":"WP Engine\u00ae","description":"Managed Hosting for WordPress","publisher":{"@id":"https:\/\/wpengine.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/wpengine.com\/#organization","name":"WP Engine","url":"https:\/\/wpengine.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/#\/schema\/logo\/image\/","url":"https:\/\/wpengine.com\/wp-content\/uploads\/2025\/09\/WPEngine_OGImage-1.webp","contentUrl":"https:\/\/wpengine.com\/wp-content\/uploads\/2025\/09\/WPEngine_OGImage-1.webp","width":1200,"height":630,"caption":"WP Engine"},"image":{"@id":"https:\/\/wpengine.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/wpengine\/","https:\/\/x.com\/wpengine","https:\/\/www.instagram.com\/wpengine\/","https:\/\/www.linkedin.com\/company\/wpengine\/","https:\/\/www.youtube.com\/channel\/UCJeAEAxX69v24CUBZ0WBYSg"]},{"@type":"Person","@id":"https:\/\/wpengine.com\/#\/schema\/person\/9625524bbe6079f6f8795d393dc2c22f","name":"Sam Rodriguez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/933722cf8761e0c08fbced6085998032df460c5ecfa2481d9cd16f569f3da2c1?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/933722cf8761e0c08fbced6085998032df460c5ecfa2481d9cd16f569f3da2c1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/933722cf8761e0c08fbced6085998032df460c5ecfa2481d9cd16f569f3da2c1?s=96&d=mm&r=g","caption":"Sam Rodriguez"},"url":"https:\/\/wpengine.com\/blog\/author\/sam-rodriguez\/"}]}},"mediapress_workflow_parent_id":null,"_links":{"self":[{"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/posts\/4318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/comments?post=4318"}],"version-history":[{"count":0,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/posts\/4318\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/media\/5482"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/media?parent=4318"}],"wp:term":[{"taxonomy":"audience","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/audience?post=4318"},{"taxonomy":"blog-category","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/blog-category?post=4318"},{"taxonomy":"buyer-stage","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/buyer-stage?post=4318"},{"taxonomy":"company-and-culture","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/company-and-culture?post=4318"},{"taxonomy":"content-type","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/content-type?post=4318"},{"taxonomy":"location","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/location?post=4318"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/persona?post=4318"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/product?post=4318"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/topic?post=4318"},{"taxonomy":"use-cases","embeddable":true,"href":"https:\/\/wpengine.com\/wp-json\/wp\/v2\/use-cases?post=4318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}