{"id":6177,"date":"2017-04-24T13:41:46","date_gmt":"2017-04-24T17:41:46","guid":{"rendered":"https:\/\/themespress.ca\/?p=6177"},"modified":"2022-09-01T19:21:25","modified_gmt":"2022-09-01T23:21:25","slug":"display-posts-taxonomy-slug","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/display-posts-taxonomy-slug\/","title":{"rendered":"Display posts related of a taxonomy slug"},"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><strong>Display posts related of a taxonomy slug:<\/strong> With this tutorial, we will create a query that will display a list of post type or posts linked to a taxonomy&#8217;s slug.<\/p>\n<p>With our example, we will create a taxonomy which represents an artist. Our Post Type will be viavideos.<\/p>\n<p>With the example below, we will store the args and taxonmy inside a variable and fetch it with post ID.<\/p>\n<p>We will now create our WordPress query for the viavideos Post Type, and then we&#8217;ll add the <strong>artist<\/strong> taxonomy&#8217;s <strong> <a href=\"https:\/\/codex.wordpress.org\/Class_Reference\/WP_Query\">tax_query<\/a><\/strong>. <\/p>\n<p>Finally we&#8217;ll add a condition that excludes the ID of the currently viewed ID.(\u2018post__not_in\u2019 =&gt; array ($post-&gt;ID)).<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-8304424\" 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-8304424\" 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\/\/ get the custom post type&#039;s taxonomy terms \n$taxonomyterms = wp_get_object_terms( $post-&gt;ID, &#039;artiste&#039;, array(&#039;fields&#039; =&gt; &#039;ids&#039;) );\n\/\/ arguments\n$args = array(\n\/\/Change you Post Type or post for posts WordPress\n&#039;post_type&#039; =&gt; &#039;viavideos&#039;, \n&#039;post_status&#039; =&gt; &#039;publish&#039;,\n&#039;posts_per_page&#039; =&gt; 3, \/\/ you may edit this number\n&#039;orderby&#039; =&gt; &#039;rand&#039;,\n&#039;tax_query&#039; =&gt; array(\n\tarray(\n\t&#039;taxonomy&#039; =&gt; &#039;artiste&#039;,\n\t&#039;field&#039;    =&gt; &#039;id&#039;,\n\t&#039;terms&#039;    =&gt; $taxonomyterms\n    )\n),\n&#039;post__not_in&#039; =&gt; array ($post-&gt;ID),\n);\n \n$related_items = new WP_Query( $args );\n\/\/ loop over query\nif ($related_items-&gt;have_posts()) : while ( $related_items-&gt;have_posts() ) : $related_items-&gt;the_post();\n?&gt;\n\/\/\/\/ Your loop\n&lt;?php endwhile; ?&gt;\n&lt;?php endif; ?&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>Adapt this query to match your needs and add it inside your loop.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"Display posts related of a taxonomy slug: With this tutorial, we will create a query that will display a list of post type or posts linked to a taxonomy's slug.<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/display-posts-taxonomy-slug\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4629,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[330],"class_list":["post-6177","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-posts"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6177","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=6177"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6177\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4629"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}