<?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, Tipps, Tutorials, Plugins and Themes</description> <lastBuildDate>Wed, 28 Jul 2010 13:37:05 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=2966</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>Quick Tipps For WordPress Query</title><link>http://wpengineer.com/quick-tipps-for-wordpress-query/</link> <comments>http://wpengineer.com/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" class="liinternal">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><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
	<span style="color: #000088;">$my_query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WP_Query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat=-21&amp;showposts=3'</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;paged='</span><span style="color: #339933;">.</span><span style="color: #000088;">$paged</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$my_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">have_posts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$my_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">the_post</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
     <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$paged</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> 
            query_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;category_name=somecat&amp;paged=<span style="color: #006699; font-weight: bold;">$paged</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div><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><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$wp_query</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div><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><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> query_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat=-2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div><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><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
	<span style="color: #000088;">$my_query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WP_Query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat=-21'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$my_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">have_posts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$my_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">the_post</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;div id=&quot;post-<span style="color: #000000; font-weight: bold;">&lt;?php</span> the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
			. . .
		&lt;/div&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
	&lt;p&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> _e<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Not found, what you are looking for.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div><h4>Own loop, which defines paged</h4><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$paged</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;cat=-2&amp;paged=<span style="color: #006699; font-weight: bold;">$paged</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div><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><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * 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
 */</span>
wp_reset_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div><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/" class="liinternal">an article </a> for the outcome of a search.</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
	&lt;h2&gt;Search Results&lt;br /&gt;&lt;small&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #666666; font-style: italic;">/* Search Count */</span>
		<span style="color: #000088;">$allsearch</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000000; font-weight: bold;">new</span> WP_Query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;s=<span style="color: #006699; font-weight: bold;">$s</span>&amp;showposts=-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> wp_specialchars<span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$allsearch</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_count</span><span style="color: #339933;">;</span>
		_e<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'term: '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$key</span><span style="color: #339933;">;</span>
		_e<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' &amp;mdash; '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
		_e<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> wp_reset_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/small&gt;&lt;/h2&gt;
&nbsp;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div><hr
/><h3>Related posts:</h3><ul><li><a
href="http://wpengineer.com/disable-wordpress-search/" rel="bookmark" title="Permanent Link: Disable WordPress Search" class="liinternal">Disable WordPress Search</a></li><li><a
href="http://wpengineer.com/quick-view-on-wordpress-settings/" rel="bookmark" title="Permanent Link: Quick View on WordPress Settings" class="liinternal">Quick View on WordPress Settings</a></li><li><a
href="http://wpengineer.com/wordpress-database-functions/" rel="bookmark" title="Permanent Link: WordPress Database Functions" class="liinternal">WordPress Database Functions</a></li><li><a
href="http://wpengineer.com/relcanonical-url-for-wordpress-theme/" rel="bookmark" title="Permanent Link: rel=&#8221;canonical&#8221;-URL for WordPress Theme" class="liinternal">rel=&#8221;canonical&#8221;-URL for WordPress Theme</a></li><li><a
href="http://wpengineer.com/more-memory-for-wordpress-more-information-about-wordpress/" rel="bookmark" title="Permanent Link: More Memory For WordPress &#8211; More Information About WordPress" class="liinternal">More Memory For WordPress &#8211; More Information About WordPress</a></li></ul><hr
/><p><img
style="float:left;" src="http://wpengineer.com/favicon.ico" alt="WP Engineer Favicon"/> Thanks for subscribing our feed! <a
href="http://buysellads.com/buy/detail/3646/" class="liexternal">Sponsor the WP Engineer Blog</a> and get your brand in front of several hundred users per day!<br
/> &copy; <a
href="http://wpengineer.com/" class="liinternal">WP Engineer Team</a>, All rights reserved <small>(Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)</small></p> ]]></content:encoded> <wfw:commentRss>http://wpengineer.com/quick-tipps-for-wordpress-query/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Database Caching 21/34 queries in 0.154 seconds using disk
Object Caching 456/519 objects using disk

Served from: wpengineer.com @ 2010-07-29 13:15:15 -->