Make a New WordPress Loop with query_posts

The “Useful WordPress Function of the Day” award goes to query_posts. This function can be used to:

  • Revise the query that WordPress forms from the URL, so you can change the sorting of posts, exclude certain categories, etc.
    query_posts($query_string . "&order=ASC&category_name=Libraries");

    This takes the current query and sorts it in ascending order, limiting the results to posts in the “Libraries” category.

  • Create custom queries, either for public-facing pages or in administration plugins.
    query_posts(array(
      "category__in" => array(1,3),
      "posts_per_page" => -1,
      "author" => 5
    ));

    This query grabs all the posts by author 5 in categories 1 or 3.

After calling query_posts, you can use your standard WordPress loop, along with all the template tags it makes available, in your template or plugin.