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.

<?php if (have_posts()) :
	$my_query = new WP_Query('cat=-21&showposts=3'.'&paged='.$paged);
	while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<?php if (have_posts()) : ?>
     <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
            query_posts("category_name=somecat&paged=$paged"); ?>
        <?php while (have_posts()) : 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.

<?php query_posts('cat=-2'); ?>

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.

<?php if ( have_posts() ) :
	$my_query = new WP_Query('cat=-21');
	while ( $my_query->have_posts() ) : $my_query->the_post();
?>
		<div id="post-<?php the_ID(); ?>">
			. . .
		</div>
	<?php endwhile; else: ?>
	<p><?php _e('Not found, what you are looking for.'); ?></p>
<?php endif; ?>

Own loop, which defines paged

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts($query_string . "&cat=-2&paged=$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
 */
wp_reset_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.

<?php if (have_posts()) : ?>

	<h2>Search Results<br /><small>
	<?php /* Search Count */
		$allsearch =& new WP_Query("s=$s&showposts=-1");
		$key = wp_specialchars($s, 1);
		$count = $allsearch->post_count;
		_e('term: ');
		echo $key;
		_e(' — ');
		echo $count . ' ';
		_e('posts'); wp_reset_query(); ?></small></h2>

	<?php while (have_posts()) : the_post(); ?>

Comments are closed.

5 comments

  1. Simon

    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

    Thanks for your advice!

  3. dgcal

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

  4. John Macpherson

    wp_reset_query() - works a treat, many thanks!

  5. Vicente

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