Custom WordPress Queries


At the bottom of my individual posts in Thesis Tips, I used to have a list of all my Thesis Tips This just checks to see if the current post is tagged ‘thesis-tip’, and that it’s not on the homepage, and if it meets those criteria then it lists all Thesis Tips.

I use this technique for lots of things. It can show the most recent posts in the footer, list of certain posts on specific pages… any time you want to call WordPress and not mess with the main content (called “The Loop”).

The Query

For information on all the parameters you can use, see WP_Query in the Codex.

For my example, I’m going to get all the posts tagged ‘thesis-tip’ and put them in a list with just the title hyperlinked to the post itself.

Make sure you include the wp_reset_postdata(), which will reset the global $post variable back to the default. If you don’t do this, WordPress will think the current page is whatever last item was in your new query, which will cause lots of issues (ex: comments from the wrong post showing up).


  1. says

    There is a spelling mistake in the line “if( $tips->hvae_posts() ):”

    “have”, instead of “hvae”!
    Just in case some will copy/paste the code. πŸ˜‰

    • Bill Erickson says

      Good catch. I’ve updated the code, so once my cache refreshes the change will be visible. Thanks!

  2. says


    This is great, really helping me understand best practices for custom queries. Quick question and maybe kinda random: what the purpose of defining ‘posts_per_page’ => ‘-1’? I’ve tried to find answers on the forums, but can’t find an explanation. Why -1?


    • Bill Erickson says

      -1 simply means “return all posts”. It indicates no limit on the number of posts per page. You could set an arbitrarily high number, like 9999, but -1 will always return all, so that’s why it’s the best practice.

  3. Nathan Schmidt says

    Is there a way to query the posts, dynamically? for instance, in your arguments where you have:

    ‘tag’ => ‘thesis-tip’,

    What if you don’t know what your ‘tag’/’term’/whatever is set to because the page is generated dynamically, for instance on a template page which could fall under any number of tags, categories or taxonomies?

    This is where I am stuck: I want my query to pull posts related to the one currently shown by its taxonomy term, but I don’t know what the term is beforehand because the template my function is used for could be outputting any number of the custom post types in my theme that fall under several different taxonomy terms.

  4. says

    Just wanted to state that this article saved my life. I have a decent grasp of custom queries, but I was trying to figure out how to set up custom loops like this. The code worked perfectly.

    Then, using this code, I found a way to run the loop multiple for each item in an array. Names of headers and classes and so on change dynamically based on array info.

    The next step is to figure out how to read data from custom taxonomies. Thanks for this and all your tutorials, Bill!

    Here is the code, for anyone who is curious. It contains a sample array, but I’ll soon replace it with a dynamic array based on the custom taxonomy meta. This code is in ‘home.php':

Leave A Reply