{"id":14191,"date":"2022-10-06T20:46:41","date_gmt":"2022-10-06T15:16:41","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/"},"modified":"2022-10-06T20:46:41","modified_gmt":"2022-10-06T15:16:41","slug":"solved-reduce-database-queries-in-code-duplicate","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/","title":{"rendered":"[Solved] Reduce Database Queries in Code [duplicate]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-88578\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"88578\" data-parentid=\"88572\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Because this is very similar to <\/p>\n<p>How to reduce the database query-es<\/p>\n<p>I will then give you an outline of a possible solution:<\/p>\n<p>You have four similar loops with<\/p>\n<pre><code>1) 1 post &amp;&amp; offset=0 \n2) 4 posts &amp;&amp; offset=1 \n3) 5 posts &amp;&amp; offset=1 \n4) 6 posts &amp;&amp; offset=1 \n<\/code><\/pre>\n<p>so your main query will be<\/p>\n<pre><code>$args=array( \n    'post_type' =&gt; 'stiri',\n    'posts_per_page' =&gt; 7,\n    'taxonomy' =&gt; 'stire',\n    'stire' =&gt; 'articole-speciale'\n);\n\n$recentPosts = new WP_Query($args);\n<\/code><\/pre>\n<p>Using that:<\/p>\n<ul>\n<li><code>$recentPosts-&gt;current_post<\/code> gives you the position <code>(0,1,...)<\/code> in<br \/>\nthe loop,<\/li>\n<li><code>$recentPosts-&gt;rewind_posts()<\/code> rewinds the posts,<\/li>\n<li><code>$recentPosts-&gt;next_post()<\/code> increments the position,<\/li>\n<\/ul>\n<p>you can try out this structure for your loop with 4 posts and offset 1:<\/p>\n<pre><code>&lt;?php $recentPosts-&gt;rewind_posts();?&gt;\n&lt;?php while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post(); ?&gt;\n    &lt;?php if($recentPosts-&gt;current_post&gt;0 &amp;&amp; $recentPosts-&gt;current_post&lt;5):?&gt;\n      ...   \n    &lt;?php endif; ?&gt;\n&lt;?php endwhile; ?&gt;\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>&lt;?php $recentPosts-&gt;rewind_posts();?&gt;\n&lt;?php $recentPosts-&gt;next_post(); \/\/ to get offset 1 ?&gt;\n&lt;?php while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post(); ?&gt;\n    &lt;?php if($recentPosts-&gt;current_post&lt;5):?&gt;\n      ...   \n    &lt;?php endif; ?&gt;\n&lt;?php endwhile; ?&gt;\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>&lt;?php $recentPosts-&gt;rewind_posts();?&gt;\n&lt;?php $recentPosts-&gt;current_post=0; \/\/ to get offset 1 ?&gt;\n&lt;?php while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post(); ?&gt;\n    &lt;?php if($recentPosts-&gt;current_post&lt;5):?&gt;\n      ...   \n    &lt;?php endif; ?&gt;\n&lt;?php endwhile; ?&gt;\n<\/code><\/pre>\n<p>You can use this on the other loops as well, where you change the <code>if<\/code>-condition to your needs.<\/p>\n<p><strong>ps:<\/strong> Finally you should use<\/p>\n<pre><code>&lt;?php wp_reset_postdata(); ?&gt;\n<\/code><\/pre>\n<p>to restore the global <code>$post<\/code> to the current post in the main query, since it was altered with the above loops.<\/p>\n<h2>Update:<\/h2>\n<p>Place this before your <code>div<\/code> code<\/p>\n<pre><code>&lt;?php\n$args=array( \n    'post_type' =&gt; 'stiri',\n    'posts_per_page' =&gt; 7,\n    'taxonomy' =&gt; 'stire',\n    'stire' =&gt; 'articole-speciale'\n);\n$recentPosts = new WP_Query($args);\n?&gt;\n<\/code><\/pre>\n<p>There is something strange in your div structure, so I give you these two examples:<\/p>\n<p>You can use this as your <code>dbr<\/code> div block:<\/p>\n<pre><code>&lt;div class=\"dbr\"&gt;\n&lt;?php $recentPosts-&gt;rewind_posts();?&gt;\n&lt;?php while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post(); ?&gt;\n    &lt;!-- #5 posts \/ offset=1 ---&gt;\n    &lt;?php if($recentPosts-&gt;current_post &gt;= 1 &amp;&amp; $recentPosts-&gt;current_post &lt;= 5):?&gt;\n        &lt;div class=\"dbrs\"&gt;\n            &lt;div class=\"dbrsi\"&gt;&lt;a href=\"https:\/\/wordpress.stackexchange.com\/questions\/88572\/&lt;?php the_permalink() ?&gt;\"&gt;&lt;img src=\"\/scripts\/timthumb.php?src=&lt;?php the_field('imagine_stire'); ?&gt;&amp;amp;h=77&amp;amp;w=218&amp;amp;zc=1\" alt=\"&lt;?php the_title(); ?&gt;\" title=\"&lt;?php the_title(); ?&gt;\" \/&gt;&lt;\/a&gt;&lt;\/div&gt;\n            &lt;div class=\"dbrst\"&gt;&lt;a href=\"https:\/\/wordpress.stackexchange.com\/questions\/88572\/&lt;?php the_permalink() ?&gt;\"&gt;&lt;?php the_title(); ?&gt;&lt;\/a&gt;&lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;?php endif; ?&gt;     \n&lt;?php endwhile; ?&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<p>and similarly for the <code>dbm<\/code> div block:<\/p>\n<pre><code>&lt;div id=\"dbm\"&gt;\n&lt;?php $recentPosts-&gt;rewind_posts();?&gt;\n&lt;?php while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post(); ?&gt;\n    &lt;!-- #4 posts \/ offset=1 ---&gt;\n    &lt;?php if($recentPosts-&gt;current_post &gt;= 1 &amp;&amp; $recentPosts-&gt;current_post &lt;= 4):?&gt;\n        &lt;div class=\"dbmc\"&gt;\n            &lt;div class=\"dbmci\"&gt;&lt;a href=\"https:\/\/wordpress.stackexchange.com\/questions\/88572\/&lt;?php the_permalink() ?&gt;\"&gt;&lt;img src=\"\/scripts\/timthumb.php?src=&lt;?php the_field('imagine_stire'); ?&gt;&amp;amp;h=77&amp;amp;w=143&amp;amp;zc=1\" alt=\"&lt;?php the_title(); ?&gt;\" title=\"&lt;?php the_title(); ?&gt;\" \/&gt;&lt;\/a&gt;&lt;\/div&gt;\n            &lt;div class=\"dbmct\"&gt;&lt;a href=\"https:\/\/wordpress.stackexchange.com\/questions\/88572\/&lt;?php the_permalink() ?&gt;\"&gt;&lt;?php the_title(); ?&gt;&lt;\/a&gt;&lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;?php endif; ?&gt;     \n&lt;?php endwhile; ?&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<p>you can then do similarly for the other two cases. Hope this helps.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Reduce Database Queries in Code [duplicate] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Because this is very similar to How to reduce the database query-es I will then give you an outline of a possible solution: You have four similar loops with 1) 1 post &amp;&amp; offset=0 2) 4 posts &amp;&amp; offset=1 3) 5 posts &amp;&amp; offset=1 4) 6 posts &amp;&amp; offset=1 so your main query will &#8230; <a title=\"[Solved] Reduce Database Queries in Code [duplicate]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/\" aria-label=\"More on [Solved] Reduce Database Queries in Code [duplicate]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[627],"tags":[628],"class_list":["post-14191","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-wp-query"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Reduce Database Queries in Code [duplicate] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Reduce Database Queries in Code [duplicate] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Because this is very similar to How to reduce the database query-es I will then give you an outline of a possible solution: You have four similar loops with 1) 1 post &amp;&amp; offset=0 2) 4 posts &amp;&amp; offset=1 3) 5 posts &amp;&amp; offset=1 4) 6 posts &amp;&amp; offset=1 so your main query will ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-06T15:16:41+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Reduce Database Queries in Code [duplicate]\",\"datePublished\":\"2022-10-06T15:16:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/\"},\"wordCount\":172,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"wp-query\"],\"articleSection\":[\"WordPress\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/\",\"name\":\"[Solved] Reduce Database Queries in Code [duplicate] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-06T15:16:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Reduce Database Queries in Code [duplicate]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Reduce Database Queries in Code [duplicate] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Reduce Database Queries in Code [duplicate] - JassWeb","og_description":"[ad_1] Because this is very similar to How to reduce the database query-es I will then give you an outline of a possible solution: You have four similar loops with 1) 1 post &amp;&amp; offset=0 2) 4 posts &amp;&amp; offset=1 3) 5 posts &amp;&amp; offset=1 4) 6 posts &amp;&amp; offset=1 so your main query will ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/","og_site_name":"JassWeb","article_published_time":"2022-10-06T15:16:41+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Reduce Database Queries in Code [duplicate]","datePublished":"2022-10-06T15:16:41+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/"},"wordCount":172,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["wp-query"],"articleSection":["WordPress"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/","url":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/","name":"[Solved] Reduce Database Queries in Code [duplicate] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-06T15:16:41+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-reduce-database-queries-in-code-duplicate\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Reduce Database Queries in Code [duplicate]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=14191"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14191\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=14191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=14191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=14191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}