<?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; Loop</title>
	<atom:link href="http://wpengineer.com/tag/loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://wpengineer.com</link>
	<description>WordPress News, Hacks, Tips, Tutorials, Plugins and Themes</description>
	<lastBuildDate>Sun, 22 Jan 2012 13:32:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Filter Duplicate Posts in the Loop</title>
		<link>http://wpengineer.com/1719/filter-duplicate-posts-in-the-loop/</link>
		<comments>http://wpengineer.com/1719/filter-duplicate-posts-in-the-loop/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 19:16:56 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[template tag]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Themes]]></category>
		<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=1719</guid>
		<description><![CDATA[As the question arises quite often I'd like to show how I make sure that the content presented, which were output in a loop, not showed up again in a second loop. WordPress identifies posts and pages via ID, which are created in the database and who also play in the output of the loop [...]]]></description>
			<content:encoded><![CDATA[<p>As the question arises quite often I'd like to show how I make sure that the content presented, which were output in a loop, not showed up again in a second loop.<br />
<span id="more-1719"></span><br />
WordPress identifies posts and pages via ID, which are created in the database and who also play in the output of the loop a crucial role. All assignments or links based on the ID. Therefore, I save in the first loop (<em>Loop No.1 </em>) the IDs, which will be output, in an array. This variable was determined in advance as an array <code>$do_not_duplicate = array();</code>.</p>
<h4>Loop no.1</h4>
<pre lang="php">
&lt;?php
$do_not_duplicate = array(); // set befor loop variable as array

// 1. Loop
query_posts(&#039;ca=1,2,3&amp;showposts=5&#039;);
while ( have_posts() ) : the_post();
    $do_not_duplicate&#091;&#093; = $post-&gt;ID; // remember ID&#039;s in loop
    // display post ...
    the_title();
endwhile;
?&gt;
</pre>
<p>After we output the above loop with 5 articles, we have in the array 5 IDs, which we can use now . You can view the array with the PHP-function <code><a href="http://de2.php.net/manual/de/function.var-dump.php">var_dump()</a></code>.</p>
<p>In the next loop no. 2 there should be 15 articles displayed, except the articles with the ID's already appearing in loop no. 1. I check with this function <code><a href="http://de2.php.net/manual/de/function.in-array.php">in_array()</a></code>,<br />
if the current ID <code>$post-&gt;ID</code> is already existing in the array <code>$do_not_duplicate</code>. Only if the ID is not existing ( <code>!in_array()</code> ) in the array, then it will be displayed in loop no. 2.</p>
<h4>Loop no.2</h4>
<pre lang="php">
&lt;?php
// 2. Loop
query_posts( &#039;cat=4,5,6&amp;showposts=15&#039; );
while (have_posts()) : the_post();
    if ( !in_array( $post-&gt;ID, $do_not_duplicate ) ) { // check IDs
// display posts ...
        the_title();
    }
endwhile;
?&gt;
</pre>
<p>But there is also an alternative, WordPress has a parameter in the query available - <code>post__not_in</code>, see <a href="http://codex.wordpress.org/Template_Tags/query_posts#Post_.26_Page_Parameters">Codex</a>.<br />
Also in this parameter, I use an array and in this query are the IDs not included. Depending on which way you go, are thus provides two different syntax, to avoid duplicate content.</p>
<pre lang="php">
&lt;?php
// another loop without duplicates
query_posts( array(
    &#039;cat&#039; =&gt; 456,
    &#039;post__not_in&#039; =&gt; $do_not_duplicate
    )
);
while ( have_posts() ) : the_post();
    // display posts...
        the_title();
endwhile;
?&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/1719/filter-duplicate-posts-in-the-loop/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Exclude Subcategories in a Loop</title>
		<link>http://wpengineer.com/876/exclude-subcategories-in-a-loop/</link>
		<comments>http://wpengineer.com/876/exclude-subcategories-in-a-loop/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 08:07:17 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[WordPress Hacks]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[Theme]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress Themes]]></category>
		<category><![CDATA[WordPress Tutorials]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://wpengineer.com/?p=876</guid>
		<description><![CDATA[This code snippet excludes the subcategories in a loop. Just insert the code in your <code>functions.php</code> of your theme or in a Plugin and you are done.]]></description>
			<content:encoded><![CDATA[<p>This code snippet excludes the subcategories in a loop. Just insert the code in your <code>functions.php</code> of your theme or in a Plugin and you are done.<br />
<span id="more-876"></span></p>
<pre lang="php">
if ( !function_exists(&#039;fb_filter_child_cats&#039;) ) {
	function fb_filter_child_cats( $cats ) {
		global $wp_query, $wpdb;

		if ( is_category() ) {

			// get children ID&#039;s
			if ( $excludes = get_categories( &quot;child_of=&quot; . $wp_query-&gt;get(&#039;cat&#039;) ) ) {

				// set array with ID&#039;s
				foreach ( $excludes as $key =&gt; $value ) {
					$exclude&#091;&#093; = $value-&gt;cat_ID;
				}
			}

			// remove child cats
			if ( isset($exclude) &amp;&amp; is_array($exclude) ) {
				$cats .= &quot; AND &quot; . $wpdb-&gt;prefix . &quot;term_taxonomy.term_id NOT IN (&quot; . implode(&quot;,&quot;, $exclude) . &quot;) &quot;;
			}
		}

		return $cats;
	}

	if ( !is_admin() ) {
		add_filter( &#039;posts_where&#039;, &#039;fb_filter_child_cats&#039; );
	}
}
</pre>
<p>Update for WP 3.1 and higher:</p>
<pre>
if ( !function_exists(&#039;fb_filter_child_cats&#039;) ) {
	function fb_filter_child_cats( $cats ) {
		global $wp_query, $wpdb;
		if ( is_category() ) {
			// get children ID&#039;s
			if ( $excludes = get_categories( &quot;child_of=&quot; . $wp_query-&gt;get(&#039;cat&#039;) ) ) {
				// set array with ID&#039;s
				foreach ( $excludes as $key =&gt; $value ) {
					$exclude&#091;&#093; = $value-&gt;cat_ID;
				}
			}
			// remove child cats
			if ( isset($exclude) &amp;&amp; is_array($exclude) ) {
				$cats .= &quot; AND &quot; . $wpdb-&gt;prefix . &quot;term_relationships.term_taxonomy_id NOT IN (&quot; . implode(&quot;,&quot;, $exclude) . &quot;) &quot;;
			}
		}
		return $cats;
	}
	if ( !is_admin() ) {
		add_filter( &#039;posts_where&#039;, &#039;fb_filter_child_cats&#039; );
	}
}
</pre>
<p>and also Update 3 for a better solution without an sql-select:</p>
<pre>
function fb_filter_child_cats($query) {

	$cat = get_term_by(&#039;name&#039;, $query-&gt;query_vars&#091;&#039;category_name&#039;&#093;, &#039;category&#039;);
	$child_cats = (array) get_term_children( &amp;$cat-&gt;term_id, &#039;category&#039; );
	// also possible
	// $child_cats = (array) get_term_children( get_cat_id($query-&gt;query_vars&#091;&#039;category_name&#039;&#093;), &#039;category&#039; );

	if ( !$query-&gt;is_admin )
		$query-&gt;set( &#039;category__not_in&#039;, array_merge($child_cats) );

	return $query;
}
add_filter( &#039;pre_get_posts&#039;, &#039;fb_filter_child_cats&#039; );
</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/876/exclude-subcategories-in-a-loop/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<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>

