{"id":4178,"date":"2022-08-21T21:42:51","date_gmt":"2022-08-21T16:12:51","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/"},"modified":"2022-08-21T21:42:51","modified_gmt":"2022-08-21T16:12:51","slug":"solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/","title":{"rendered":"[Solved] Some doubts about how the main query and the custom query works in this custom theme?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-155976\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"155976\" data-parentid=\"155956\" data-score=\"35\" 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>Your actual question is basically when to run a custom query and when to make use of the main query. Lets break it down in three parts<\/p>\n<p><strong>PART ONE<\/strong><\/p>\n<p>When to run a custom query (This is not a definitive list)<\/p>\n<ul>\n<li>\n<p>To create custom content sliders<\/p>\n<\/li>\n<li>\n<p>To create a featured content area in a page<\/p>\n<\/li>\n<li>\n<p><strike>On page.php templates if you need to display posts<\/strike><\/p>\n<\/li>\n<li>\n<p><strike>If you require custom content on a static front page<\/strike><\/p>\n<\/li>\n<li>\n<p>Display related, popular or informational posts<\/p>\n<\/li>\n<li>\n<p>Any other secondary or supplementary content outside the scope of the main query<\/p>\n<\/li>\n<\/ul>\n<p>When to make use of the main query.<\/p>\n<p>To display the primary content on<\/p>\n<ul>\n<li>\n<p>On your homepage and the page set as a blogpage in the backend<\/p>\n<\/li>\n<li>\n<p>All archive pages which includes templates like archive.php, category.php, author.php, taxonomy.php, tag.php and date.php<\/p>\n<\/li>\n<li>\n<p><em><strong>UPDATE:<\/strong><\/em> Display custom content on true pages and a static front page (<em>see Using pre_get_posts on true pages and static front pages<\/em>)<\/p>\n<\/li>\n<\/ul>\n<p><strong>PART TWO<\/strong><\/p>\n<blockquote>\n<p>To select all the featured posts I use this line that create a new WP_Query object that define a query having the specific tag featured:<\/p>\n<p>So, from what I have understand, this is not the WordPres main query but it is a new query created by me. From what I have understand it is better create a new query (as done) and not use the main query when I want perform this kind of operations<\/p>\n<\/blockquote>\n<p>Correct. This falls out of scope for the main query. This is secondary or supplementary content which cannot be created with the main query. You <strong>SHOULD ALWAYS<\/strong> use either <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/codex.wordpress.org\/Class_Reference\/WP_Query\"><code>WP_Query<\/code><\/a> or <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/codex.wordpress.org\/Template_Tags\/get_posts\"><code>get_posts<\/code><\/a> to create your custom queries.<\/p>\n<p><strong>NEVER USE<\/strong> <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/codex.wordpress.org\/Function_Reference\/query_posts\"><code>query_posts<\/code><\/a> to create custom queries, or even any other query. My emphasis.<\/p>\n<blockquote>\n<p><strong>Note:<\/strong> This function isn&#8217;t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).<\/p>\n<\/blockquote>\n<p>Moving on<\/p>\n<blockquote>\n<p>Ok, going on I show all the posts that have not the featured tag, to do this I use this code snippet that on the contrary modify the main query:<\/p>\n<pre><code>query_posts( array( 'tag__not_in' =&gt; array ( $term-&gt;term_id )));\n<\/code><\/pre>\n<p>So I think that this is pretty horrible. Is it true?<\/p>\n<\/blockquote>\n<p>That is all wrong and your statement is unfortunately true. As said before, <strong>NEVER<\/strong> use <code>query_posts<\/code>. It runs a complete new query, which is bad for performance, and it most cases breaks pagination which is an integral part of the main query for pagination to work correctly.<\/p>\n<p>This is your primary content, so you should be using the main query with the default loop, which should look like this, and this is all you need<\/p>\n<pre><code>&lt;?php\n    if (have_posts()) :\n        \/\/ Start the Loop.\n        while (have_posts()) : the_post();\n\n            get_template_part('content', get_post_format());\n\n        endwhile;\n    else :\n        \/\/ If no content, include the \"No posts found\" template.\n        get_template_part('content', 'none');\n\n    endif;\n?&gt;\n<\/code><\/pre>\n<p>You can completely get rid of this part, delete it, burn it and forget about it<\/p>\n<pre><code>&lt;?\n\/\/ get the term using the slug and the tag taxonomy\n$term = get_term_by( 'slug', 'featured', 'post_tag' );\n\/\/ pass the term_id to tag__not_in\nquery_posts( array( 'tag__not_in' =&gt; array ( $term-&gt;term_id )));\n?&gt;\n<\/code><\/pre>\n<p>OK, once you&#8217;ve done that, you&#8217;ll see that posts from the feature tag appear in your home page using the main query and default loop.<\/p>\n<p>The correct way of removing this tag from the homepage is with <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference\/pre_get_posts\"><code>pre_get_posts<\/code><\/a>. This is the <strong>proper way to alter the main query<\/strong> and the hook you should <strong>always<\/strong> use to make changes to your primary content loop.<\/p>\n<p>So, the code with <code>pre_get_posts<\/code> is correct and this is the function that you should use. Just one thing, always do a check that you are not on an admin page because <code>pre_get_posts<\/code> alters the back end as well. So this is the proper code to use in <code>functions.php<\/code> to remove posts tagged <em>featured<\/em> from the homepage<\/p>\n<pre><code>add_action( 'pre_get_posts', 'exclude_featured_tag' );\nfunction exclude_featured_tag( $query ) \n{\n    if (    !is_admin() \n         &amp;&amp; $query-&gt;is_home() \n         &amp;&amp; $query-&gt;is_main_query() \n    ) {\n        $query-&gt;set( 'tag__not_in', [ID OF THE FEATURED TAG] );\n    }\n}\n<\/code><\/pre>\n<p><strong>PART THREE<\/strong><\/p>\n<p>Extra reading material which will be helpful in future<\/p>\n<ul>\n<li>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/codex.wordpress.org\/Conditional_Tags\">Conditional tags<\/a><\/p>\n<\/li>\n<li>\n<p>When should you use WP_Query vs query_posts() vs get_posts()?<\/p>\n<\/li>\n<li>\n<p>When to use WP_query(), query_posts() and pre_get_posts<\/p>\n<\/li>\n<li>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/codex.wordpress.org\/Query_Overview\">Query Overview<\/a><\/p>\n<\/li>\n<li>\n<p>Guidance with The Loop for CMS<\/p>\n<\/li>\n<\/ul>\n<\/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 Some doubts about how the main query and the custom query works in this custom theme? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Your actual question is basically when to run a custom query and when to make use of the main query. Lets break it down in three parts PART ONE When to run a custom query (This is not a definitive list) To create custom content sliders To create a featured content area in a &#8230; <a title=\"[Solved] Some doubts about how the main query and the custom query works in this custom theme?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/\" aria-label=\"More on [Solved] Some doubts about how the main query and the custom query works in this custom theme?\">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":[637,629,628],"class_list":["post-4178","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-loop","tag-query-posts","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] Some doubts about how the main query and the custom query works in this custom theme? - 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-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Some doubts about how the main query and the custom query works in this custom theme? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Your actual question is basically when to run a custom query and when to make use of the main query. Lets break it down in three parts PART ONE When to run a custom query (This is not a definitive list) To create custom content sliders To create a featured content area in a ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-21T16:12:51+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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Some doubts about how the main query and the custom query works in this custom theme?\",\"datePublished\":\"2022-08-21T16:12:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/\"},\"wordCount\":693,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"loop\",\"query-posts\",\"wp-query\"],\"articleSection\":[\"WordPress\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/\",\"name\":\"[Solved] Some doubts about how the main query and the custom query works in this custom theme? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-21T16:12:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Some doubts about how the main query and the custom query works in this custom theme?\"}]},{\"@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] Some doubts about how the main query and the custom query works in this custom theme? - 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-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Some doubts about how the main query and the custom query works in this custom theme? - JassWeb","og_description":"[ad_1] Your actual question is basically when to run a custom query and when to make use of the main query. Lets break it down in three parts PART ONE When to run a custom query (This is not a definitive list) To create custom content sliders To create a featured content area in a ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/","og_site_name":"JassWeb","article_published_time":"2022-08-21T16:12:51+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Some doubts about how the main query and the custom query works in this custom theme?","datePublished":"2022-08-21T16:12:51+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/"},"wordCount":693,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["loop","query-posts","wp-query"],"articleSection":["WordPress"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/","url":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/","name":"[Solved] Some doubts about how the main query and the custom query works in this custom theme? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-21T16:12:51+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-some-doubts-about-how-the-main-query-and-the-custom-query-works-in-this-custom-theme\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Some doubts about how the main query and the custom query works in this custom theme?"}]},{"@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\/4178","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=4178"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4178\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=4178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=4178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=4178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}