Quick Tipps For WordPress Query

The loop of WordPress is a powerful tool. Most users just use the standard loop. But you can adjust and use loops very good for your requirements. I showed an example in my last post for feeds.

I will show some other examples, which are barely used, but often asked by users, in this post. Short and sweet, learn and use.

Page break with wp-query

If you use own loops, which are defined through the variable $wp_query, created by WP-Query, then it won’t display any pagination. Two examples with different ways to approach.

have_posts() ) : $my_query->the_post();


Everybody who likes to create a special case with the loop, should check out the array of this variable more closely. Because many useful possibilities are available and can replace other solutions. For example this one with a simple syntax:

var_dump( $wp_query );

For PHP users nothing new, but maybe for some theme authors.

Exclude categories in a loop

Very often you like to exclude a category in a loop. A typical example is, if you like to have a side- or tumbler-blog within your WordPress installation. You don’t need a Plugin for that. You can select a category, which will be displayed via extra loop in your sidebar, to display short tips and tricks. In your main blog you just exclude this category, so only posts with more content will be listed.

A standard loop, which excludes category with ID 21

The easiest way to build a loop, take out the category and so the pagination. Below you see a loop, where you just have to fill in some content.

have_posts() ) : $my_query->the_post();
. . .

Own loop, which defines paged

Reset query

Problems with your own query? If you use several loops and there is an error coming up, it helps to reset. Not much documented but in my opinion very helpful. I use this, if I load several queries or have a problem in the output.

Putting it before or after a query query_posts(), works like magic sometimes.

 * Destroy the previous query and setup a new query.
 * This should be used after {@link query_posts()} and before another {@link
 * query_posts()}. This will remove obscure bugs that occur when the previous
 * wp_query object is not destroyed properly before another is setup.
 * @since 2.3.0
 * @uses $wp_query

A small example for that. The first loop gives you the number of posts back, which contains the search. After that I put a reset and continue with standard loop to display the outcome. I wrote an article for the outcome of a search.


Search Results
post_count; _e('term: '); echo $key; _e(' — '); echo $count . ' '; _e('posts'); wp_reset_query(); ?>





5 responses to “Quick Tipps For WordPress Query”

  1. Simon Avatar

    If you want to use tags in the loop, you have to add this line:

    global $wp_query; $wp_query->in_the_loop = true;

  2. Frank Avatar

    Thanks for your advice!

  3. dgcal Avatar

    Just discovered this site through a link from themeforest. Great site! I will bookmark this very useful page!

  4. John Macpherson Avatar

    wp_reset_query() – works a treat, many thanks!

  5. Vicente Avatar

    Hello! What should I do if I want to exclude the sticky posts at the top of index?