WordPress Querying posts


  • $the_query = new WP_Query( $args );
  • $posts_array = get_posts( $args );


$args(array) An array of needed arguments for a query - can be custom tailored to your needs, e.g. querying posts from only one category, from custom post type or even querying certain taxonomy


Query arguments are numerous. WP_Query() codex page has a list of parameters. Some of them are

One of the most important thing to have in mind is:

Never use query_posts()

query_posts() overrides the main query, and can cause problems in the rest of your theme. Any time you need to modify the main query (or any query for that matter) is to use pre_get_posts filter. This will allow you to modify the query before it ran.

Also when you are querying posts, you should always reset it using wp_reset_postdata(). This will restore the global $post variable of the main query loop, and you won't have any issues later on (such as categories being excluded, because in your secondary loop you've excluded them and forgot to reset the query).