{"id":170,"date":"2011-11-18T13:44:26","date_gmt":"2011-11-18T19:44:26","guid":{"rendered":"http:\/\/www.marcblase.com\/blog\/?p=170"},"modified":"2012-01-23T12:36:16","modified_gmt":"2012-01-23T18:36:16","slug":"wordpress-custom-queries-need-resetting-yhear","status":"publish","type":"post","link":"https:\/\/ma.rcbla.se\/blog\/2011\/11\/wordpress-custom-queries-need-resetting-yhear\/","title":{"rendered":"WordPress custom queries need resetting, y&#8217;hear?"},"content":{"rendered":"<p>So, as I often do when building WP themes, I make custom queries. Something along the lines of:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$custom_query = new WP_Query('category_name=category_name&amp;showposts=3');\r\nwhile ($custom_query-&gt;have_posts()) : $custom_query-&gt;the_post();\r\n\/\/ Do fancy layout here\r\nendwhile;\r\nif(is_page())\r\n \/\/ Do page stuff - DOESN'T WORK!!!\r\n<\/pre>\n<p>This is all well and good, but if you are using conditionals below this custom query, as we are in the example above, they will not work. At all. So, WordPress devs knew we&#8217;d need a construct  for this. If you haven&#8217;t met, let me introduce you to <a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/wp_reset_query\" target=\"_blank\"><code>wp_reset_query()<\/code><\/a>. Add it after you close your loop and you&#8217;re all set to is_page(), is_archive(), etc.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$custom_query = new WP_Query('category_name=category_name&amp;showposts=3');\r\nwhile ($custom_query-&gt;have_posts()) : $custom_query-&gt;the_post();\r\n\/\/ Do fancy layout here\r\nendwhile;\r\nwp_reset_query();\r\n\/\/ Do conditionals ...\r\nif (is_page())\r\n    \/\/ Do page stuff - Totally works!\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So, as I often do when building WP themes, I make custom queries. Something along the lines of: $custom_query = new WP_Query(&#8216;category_name=category_name&amp;showposts=3&#8217;); while ($custom_query-&gt;have_posts()) : $custom_query-&gt;the_post(); \/\/ Do fancy layout here endwhile; if(is_page()) \/\/ Do page stuff &#8211; DOESN&#8217;T WORK!!! This is all well and good, but if you are using conditionals below this custom [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,3],"tags":[],"class_list":["post-170","post","type-post","status-publish","format-standard","hentry","category-coding","category-discoveries"],"_links":{"self":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/comments?post=170"}],"version-history":[{"count":8,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/170\/revisions\/195"}],"wp:attachment":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}