<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WP Engineer &#187; Query</title>
	<atom:link href="http://wpengineer.com/tag/query/feed/" rel="self" type="application/rss+xml" />
	<link>http://wpengineer.com</link>
	<description>WordPress News, Hacks, Tips, Tutorials, Plugins and Themes</description>
	<lastBuildDate>Mon, 21 May 2012 22:48:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Quick Tipps For WordPress Query</title>
		<link>http://wpengineer.com/305/quick-tipps-for-wordpress-query/</link>
		<comments>http://wpengineer.com/305/quick-tipps-for-wordpress-query/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 20:27:52 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[Query]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Tutorials]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=305</guid>
		<description><![CDATA[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. [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://wpengineer.com/?p=287">last post for feeds</a>.</p>
<p>I will show some other examples, which are barely used, but often asked by users, in this post. Short and sweet, learn and use.<br />
<span id="more-305"></span></p>
<h3>Page break with wp-query</h3>
<p>If you use own loops, which are defined through the variable <code>$wp_query</code>, created by WP-Query, then it won't display any pagination. Two examples with different ways to approach.</p>
<pre lang="php">
&lt;?php if (have_posts()) :
	$my_query = new WP_Query(&#039;cat=-21&amp;showposts=3&#039;.&#039;&amp;paged=&#039;.$paged);
	while ( $my_query-&gt;have_posts() ) : $my_query-&gt;the_post();
?&gt;
</pre>
<pre lang="php">
&lt;?php if (have_posts()) : ?&gt;
     &lt;?php $paged = (get_query_var(&#039;paged&#039;)) ? get_query_var(&#039;paged&#039;) : 1;
            query_posts(&quot;category_name=somecat&amp;paged=$paged&quot;); ?&gt;
        &lt;?php while (have_posts()) : the_post(); ?&gt;
</pre>
<p>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:</p>
<pre lang="php">var_dump( $wp_query );</pre>
<p>For PHP users nothing new, but maybe for some theme authors.</p>
<h3>Exclude categories in a loop</h3>
<p>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.</p>
<pre lang="php">
&lt;?php query_posts(&#039;cat=-2&#039;); ?&gt;
</pre>
<h4>A standard loop, which excludes category with ID 21</h4>
<p>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.</p>
<pre lang="php">
&lt;?php if ( have_posts() ) :
	$my_query = new WP_Query(&#039;cat=-21&#039;);
	while ( $my_query-&gt;have_posts() ) : $my_query-&gt;the_post();
?&gt;
		&lt;div id=&quot;post-&lt;?php the_ID(); ?&gt;&quot;&gt;
			. . .
		&lt;/div&gt;
	&lt;?php endwhile; else: ?&gt;
	&lt;p&gt;&lt;?php _e(&#039;Not found, what you are looking for.&#039;); ?&gt;&lt;/p&gt;
&lt;?php endif; ?&gt;
</pre>
<h4>Own loop, which defines paged</h4>
<pre lang="php">
&lt;?php
$paged = (get_query_var(&#039;paged&#039;)) ? get_query_var(&#039;paged&#039;) : 1;
query_posts($query_string . &quot;&amp;cat=-2&amp;paged=$paged&quot;);
?&gt;
</pre>
<h3>Reset query</h3>
<p>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.</p>
<p>Putting it before or after a query <code>query_posts()</code>, works like magic sometimes.</p>
<pre lang="php">
/**
 * 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()
</pre>
<p>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  <a href="http://wpengineer.com/expand-search-results-in-wordpress/">an article </a> for the outcome of a search.</p>
<pre lang="php">
&lt;?php if (have_posts()) : ?&gt;

	&lt;h2&gt;Search Results&lt;br /&gt;&lt;small&gt;
	&lt;?php /* Search Count */
		$allsearch =&amp; new WP_Query(&quot;s=$s&amp;showposts=-1&quot;);
		$key = wp_specialchars($s, 1);
		$count = $allsearch-&gt;post_count;
		_e(&#039;term: &#039;);
		echo $key;
		_e(&#039; &mdash; &#039;);
		echo $count . &#039; &#039;;
		_e(&#039;posts&#039;); wp_reset_query(); ?&gt;&lt;/small&gt;&lt;/h2&gt;

	&lt;?php while (have_posts()) : the_post(); ?&gt;
</pre>
<hr /><a href="http://wpplugins.com/plugin/281/snippets" title="More informations about this plugin for WordPress"><img src="http://wpengineer.com/wp-content/themes/wpe-3/images/snippets-125-125.png" height="90" alt="WordPress Snippet Plugin" /></a> <a href="http://xtreme-theme.com"><img src="http://wpengineer.com/wp-content/uploads/feed-banner-2.jpg" alt="Xtreme One WordPress Framework"/></a><br />
&copy; <a href="http://wpengineer.com/">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://wpengineer.com/305/quick-tipps-for-wordpress-query/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

