{"id":972,"date":"2009-07-13T12:12:49","date_gmt":"2009-07-13T11:12:49","guid":{"rendered":"http:\/\/www.maratz.com\/blog\/?p=972"},"modified":"2012-12-23T16:57:50","modified_gmt":"2012-12-23T15:57:50","slug":"exclude-articles-from-a-category-tree-on-your-wordpress-homepage","status":"publish","type":"post","link":"http:\/\/www.maratz.com\/blog\/archives\/2009\/07\/13\/exclude-articles-from-a-category-tree-on-your-wordpress-homepage\/","title":{"rendered":"Exclude articles from a category tree on your WordPress homepage"},"content":{"rendered":"<p>A nice way to manage articles visible only to registered users on your <a href=\"http:\/\/wordpress.org\/\">WordPress<\/a> based site is to create one parent category exclusively for those posts and then create sub categories if needed.<\/p>\n<p>Once you have the top, subscriber-only category, you will most likely not change it anytime soon, and therefore you can use its category ID as an argument to construct your query, for instance <code>query_posts(cat=-3)<\/code>.<\/p>\n<p>In this example all posts from the category with ID 3 are excluded from the listing. However, this doesn\u2019t exclude posts in child categories, and if we knew all the IDs, we\u2019d expand the call to function like so <code>query_posts(cat=-3,-4,-5)<\/code>.<\/p>\n<p>In the real life, there are two possible issues. First, it\u2019s a bad practice to rely on IDs for child categories, and the second one is that the default argument syntax <code>cat=-3<\/code> won\u2019t always work, due to plugins incompatibilities.<!--more--><\/p>\n<h2>How to get the child categories IDs?<\/h2>\n<p>We can use <code><a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/get_categories\">get_categories<\/a><\/code> to get the sub categories. In the following example we will first create a string, which we\u2019d use with <code>query_posts(cat=' . $subcat_string)<\/code>, but aforementioned issues with plugins could require a different argument construction.<\/p>\n<p><code><\/p>\n<pre>&#60;?php \r\n\r\n\r\n    \/\/ In this example, the parent category ID is 3\r\n    <strong>$subcategories = get_categories('child_of=3');<\/strong>\r\n    \r\n    <strong>$subcat_string = '';\r\n    foreach ($subcategories as $subcat) {\r\n        $subcat_string .= '-' . $subcat->cat_ID . ',';\r\n    }<\/strong>\r\n    \r\n    \/\/ we also include parent category ID in the list\r\n    <strong>$subcat_string .= '-3';<\/strong>\r\n                                   \r\n    \/\/ and then call query_posts\r\n    <strong>query_posts(cat=' . $subcat_string);<\/strong>\r\n    \r\n?&#62;<\/pre>\n<p><\/code><\/p>\n<p>A slightly more advanced (for designers at least) <code>query_posts()<\/code> call is to use an array for arguments. Quite often where <code>cat=-n<\/code> doesn\u2019t work this should fix the problem. I believe this is due to plugins incompatibility, but it could be also due to WordPress itself. Whatever the problem is, this is one way to handle it:<\/p>\n<p><code><\/p>\n<pre>&#60;?php \r\n\r\n    \/\/ Subcategories IDs as an array\r\n\r\n    \/\/ In this example, the parent category ID is 3\r\n    <strong>$subcategories = get_categories('child_of=3');<\/strong>\r\n    \r\n    <strong>$subcat_array = array();\r\n    foreach ($subcategories as $subcat) {\r\n        $subcat_array[] = '-' . $subcat->cat_ID;\r\n    }<\/strong>\r\n    \r\n    \/\/ we also include parent category ID in the list\r\n    <strong>$subcat_array[] = '-3';<\/strong>\r\n                                   \r\n    \/\/ and then call query_posts\r\n    <strong>query_posts(array('category__not_in' => $subcat_array));<\/strong>\r\n    \r\n?&#62;<\/pre>\n<p><\/code> <\/p>\n<h2>Hide posts from the members-only categories<\/h2>\n<p>To check if a visitor is logged-in or not, you\u2019d test <code>$user_ID<\/code> variable. If the visitor wasn\u2019t a member, we would display latest articles sans members-only category. For signed-in users we would display the default query results, i.e. all articles from all categories.<\/p>\n<p><code><\/p>\n<pre>&#60;?php\r\n     \r\n<strong>if (!$user_ID) {<\/strong>\r\n     \r\n     \/\/ we created an array of categories to exclude:\r\n     $subcategories = get_categories('child_of=3');\r\n     $subcat_array = array();\r\n     foreach ($subcategories as $subcat) {\r\n         $subcat_array[] = '-' . $subcat->cat_ID;\r\n     }\r\n     $subcat_array[] = '-3';\r\n\r\n     \/\/ Here, we set $limit for showposts (so it can be redefined via admin if needed)\r\n     <strong>$limit = get_option('posts_per_page');<\/strong>\r\n\r\n     \/\/ ... and $paged (to tell WP which page in the Loop is current)\r\n     <strong>$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;<\/strong> \r\n\r\n     \/\/ then populate arguments\r\n     <strong>$args = array(\r\n         'category__not_in' => $subcat_array,\r\n         'paged' => $paged,\r\n         'showposts' => $limit\r\n     );<\/strong>\r\n\r\n     \/\/ and call the function\r\n     <strong>query_posts($args);<\/strong>\r\n\r\n     \/\/ the general loop code goes here\r\n     \r\n<strong>}<\/strong>\r\n                      \r\n?&#62;<\/pre>\n<p><\/code><\/p>\n<p>Got something to add? Speak up!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A nice way to manage articles visible only to registered users on your WordPress based site is to create one parent category exclusively for those posts and then create sub categories if needed. Once you have the top, subscriber-only category, you will most likely not change it anytime soon, and therefore you can use its [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[],"_links":{"self":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/972"}],"collection":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/comments?post=972"}],"version-history":[{"count":0,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/posts\/972\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/media?parent=972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/categories?post=972"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.maratz.com\/blog\/wp-json\/wp\/v2\/tags?post=972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}