{"id":6270,"date":"2016-10-11T18:11:52","date_gmt":"2016-10-11T22:11:52","guid":{"rendered":"https:\/\/themespress.ca\/?p=6270"},"modified":"2022-09-07T21:08:14","modified_gmt":"2022-09-08T01:08:14","slug":"excluding-the-current-id-with-a-query-post-type","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/excluding-the-current-id-with-a-query-post-type\/","title":{"rendered":"Excluding the Current ID With a Query Post Type"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p> With this WordPress tutorial, we will create a query that will allow you to exclude the current ID from your Post Type.<\/p>\n<p>In our example, we already have a Post Type called <strong>Services<\/strong>. Let&#8217;s say we want to consult one of our services, WordPress will get the data from the <strong>single-services.php<\/strong> file and if WordPress doesn&#8217;t find this file, it will call <strong>single.php<\/strong>.<\/p>\n<p>If you don&#8217;t have a <strong>single-posttype.php<\/strong> file (Ex:single-services.php), make sure to create it if you want to be able to customize it with HTML and CSS.<\/p>\n<p>Once your file is ready, let&#8217;s add our Query Post Type in our sidebar DIV by excluding the current post ID.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Excluding the Current ID With a Query Post Type<\/h2><div id=\"ts-enlighterjs-container-7162310\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-7162310\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php \n$i = 1; \n$currentID = get_the_ID(); \n$temp = $wp_query; \n$wp_query = null; \n$wp_query = new WP_Query( \n\tarray( \n\t\t&#039;post_type&#039;      =&gt; &#039;services&#039;, \n\t\t&#039;showposts&#039;      =&gt; &#039;5&#039;, \n\t\t&#039;post__not_in&#039;   =&gt; array($currentID) \n\t)\n); \n\nwhile ($wp_query-&gt;have_posts()) : $wp_query-&gt;the_post(); ?&gt;\n\n\/\/\/ Votre loop ici \n&lt;?php echo $i; ?&gt;\n\n&lt;?php $i++; endwhile; ?&gt;\n&lt;?php \n$wp_query = null; $wp_query = $temp; \n\/\/ Reset \n?&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>The first line, <strong>$i = 1;<\/strong>, will allow you to increment by 1 and starting from 1. This means we wil count by number the query order of your services. So if your loop, you can display the sorting number this way:<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-1146192\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-1146192\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php echo $i; ?&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>In our second line of our <strong>Query Post Type<\/strong>, we will store the current ID in a variable in order to exclude it with post_not_in (&#8216;post__not_in&#8217; => array($currentID)).<\/p>\n<p>Reference: <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_the_id\/\" target=\"_blank\" rel=\"noopener\">get_the_ID();<\/a><br \/>\nThis will display the results of your Post Type in the sidebar except the current page.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this WordPress tutorial, we will create a query that will allow you to exclude the current ID from your Post Type. In our example, we already have a Post<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/excluding-the-current-id-with-a-query-post-type\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4544,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[320,322],"class_list":["post-6270","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-php-coding","tag-query"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=6270"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6270\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4544"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}