{"id":1592903,"date":"2010-07-21T13:57:48","date_gmt":"2010-07-21T13:57:48","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/nested-loop-problem\/"},"modified":"2016-08-19T18:47:05","modified_gmt":"2016-08-19T18:47:05","slug":"nested-loop-problem","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/nested-loop-problem\/","title":{"rendered":"Nested Loop problem"},"content":{"rendered":"<p>Hey everyone,<\/p>\n<p>I&#8217;m trying to do a nested loop in WordPress.  I can get the start of the outer loop, and the content of the inner loop to display fine.  But I can&#8217;t get it to snap back to the original loop afterwards.  It just proceeds as though it&#8217;s still in the inner loop.<\/p>\n<p>Important bits<\/p>\n<pre><code>$associatedPosts = $newsletter-&gt;get_related_posts();\n\/\/Start the sub-loop &amp; save the original.\n$temp_query = clone $wp_query;\n$sub_loop_1 = new WP_Query(array(&#039;post__in&#039; =&gt; $associatedPosts));\nwhile ($sub_loop_1-&gt;have_posts()) : $sub_loop_1-&gt;the_post();\n?&gt;\n&lt;h3&gt;&lt;?php the_title(); ?&gt;&lt;\/h3&gt;\n&lt;div class=&quot;entry-content&quot;&gt;\n&lt;?php\nif(has_post_thumbnail()) {\n\tthe_post_thumbnail();\n}\n?&gt;\n&lt;?php the_excerpt(); ?&gt;\n&lt;\/div&gt;\n&lt;hr style=&quot;border: 1px solid black;&quot; \/&gt;\n&lt;?php\nendwhile;\n$wp_query = clone $temp_query;\n?&gt;<\/code><\/pre>\n<p>I tried cloning the wp_query object, but that doesn&#8217;t have any effect.  For instance, the last post in this inner loop is the &#8220;hello world&#8221; post.  So at the bottom of the page, the comments for &#8220;hello world&#8221; are shown, instead of the comments for the post in the outer loop.<\/p>\n<p>Any help would be much appreciated.  Thanks!<\/p>\n","protected":false},"template":"","class_list":["post-1592903","topic","type-topic","status-closed","hentry","topic-tag-get_post","topic-tag-loop","topic-tag-nested-loop"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1592903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1592903\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=1592903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}