{"id":141,"date":"2005-11-01T14:39:30","date_gmt":"2005-11-01T19:39:30","guid":{"rendered":"http:\/\/ronrothman.com\/public\/leftbraned\/?p=141"},"modified":"2019-02-15T20:56:28","modified_gmt":"2019-02-16T01:56:28","slug":"wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link","status":"publish","type":"post","link":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/","title":{"rendered":"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link()"},"content":{"rendered":"<div style=\"padding: 4px; border: 2px solid maroon; background-color: #fed;\">(UPDATE: Word on the street (see <a href=\"#comment-27\">comment 27<\/a>, below) is that this bug has been fixed in WordPress 2.1.)<\/div>\n<p>I recently came across a known bug in WordPress 1.5.2 (well, at least it&#8217;s <a href=\"https:\/\/wordpress.org\/support\/topic\/44031\">known<\/a> to users; I can only assume that the dev community is also aware of it).  Just wanted to post my fix here, in case anyone else finds it helpful.<\/p>\n<h3>The Bug<\/h3>\n<p><strong>Category exclusion<\/strong> is broken.  The problem is that the category exclusion parameter (the 4th param) of <strong><code>next_post_link()<\/code><\/strong> and <strong><code>previous_post_link()<\/code><\/strong> is effectively ignored.<br \/>\n<!--more--><br \/>\n(<code>next_post_link()<\/code> and <code>previous_post_link()<\/code> are the new versions of the now-deprecated <code>next_post()<\/code> and <code>previous_post()<\/code>.)<\/p>\n<p>A quick scan through the WordPress code indicates that the &#8220;category exclude&#8221; feature isn&#8217;t even implemented (at least not in a serious way) for next\/previous_post_link.  I haven&#8217;t spent much time perusing the WordPress code base, but from what I can tell, it seems that the relevant sections of these functions were never upgraded from an earlier WP version&#8211;one with a different schema; in particular, one with a different method of storing post-category relationships.<\/p>\n<h3>The <span style=\"text-decoration: line-through;\">Fix<\/span>Hack<\/h3>\n<p>Here is how I fixed it in my installation (<strong>see warnings below<\/strong> before trying this at home):<\/p>\n<p>1.  Edit <code>wp-includes\/template-functions-links.php<\/code>.  At approximately line 253 (in function <code>get_previous_post<\/code>) [line 221 in WP1.5], you&#8217;ll see this:<\/p>\n<pre class=\"code\">    $sql_exclude_cats = '';\n    if (!empty($excluded_categories)) {\n        $blah = explode('and', $excluded_categories);\n        foreach($blah as $category) {\n            $category = intval($category);\n            $sql_exclude_cats .= \" AND post_category != $category\";\n        }\n    }\n<\/pre>\n<p>2.  Replace that block with the following (<strong>beware of copy\/paste errors<\/strong>, especially with respect to line breaks):<\/p>\n<pre class=\"code\">    $sql_exclude_cats = '';\n    if (!empty($excluded_categories)) {\n### ronr hack BEGIN ###\n        # find ids of all posts in excluded cats\n        $query =\n            \"SELECT post_id\n             FROM $wpdb-&gt;post2cat\n             WHERE category_id IN (\"\n             . str_replace(',', ',', $excluded_categories)\n             . ')';\n        $excluded_post_rows = $wpdb-&gt;get_results($query);\n\n        # build IN clause from those ids\n        if (count($excluded_post_rows) &gt; 0) {\n            $sql_exclude_cats =\n                ' AND ID NOT IN ('\n                . intval($excluded_post_rows[0]-&gt;post_id);\n            for ($i = 1; $i &lt; (count($excluded_post_rows)); $i++) {\n                $sql_exclude_cats .=\n                    ','\n                    . intval($excluded_post_rows[$i]-&gt;post_id);\n            }\n            $sql_exclude_cats .= \" )\";\n        }\n### ronr hack END ###\n    }\n<\/pre>\n<p>(Note: instead of copy\/pasting the above code, you can download it <a href=\"https:\/\/www.ronrothman.com\/public\/lb\/code\/next_post_link_hack.php\">here<\/a>.)<\/p>\n<p>3.  Now, <strong>make the same replacement<\/strong> in the corresponding block in <code>get_next_post<\/code>, which is a little further down in the file.<\/p>\n<h3>Disclaimers<\/h3>\n<p>Presumably, this bug will be fixed properly in an upcoming release, so now that I&#8217;ve got it working (albeit jury-rigged), I&#8217;m not going to spend any more time on it.  If it doesn&#8217;t get fixed soon, perhaps I&#8217;ll clean it up and fold it into a plug-in (perhaps even as Asides plug-in).<\/p>\n<p>My code does <strong>no error checking<\/strong>, and it <strong>assumes<\/strong> that the value of the exclude_categories parameter is not screwy.  It&#8217;s provided as-is.  I would never release code like this if it wasn&#8217;t intended to be used solely by me.  If I wrote code like this at work, I&#8217;d deserve to be fired.  Constructive suggestions for improvements are always welcome, but please don&#8217;t bitch to me about how it didn&#8217;t work for you, broke your blog, crashed your server, ruined your marriage, etc.  <i>Capisce?<\/i><\/p>\n<p>Note too that I&#8217;ve changed the original interface; instead of separating excluded category ids with the word &#8220;and&#8221; (odd), it now expects a comma (not odd).<\/p>\n<p>Anyway, good luck.  And if you happen to find this useful, please <a href=\"#comment\">let me know<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(UPDATE: Word on the street (see comment 27, below) is that this bug has been fixed in WordPress 2.1.) I recently came across a known bug in WordPress 1.5.2 (well, at least it&#8217;s known to users; I can only assume that the dev community is also aware of it). Just wanted to post my fix [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[11,5],"tags":[61,54,53],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link() - \u00ableftbraned<\/title>\n<meta name=\"description\" content=\"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link()\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link() - \u00ableftbraned\" \/>\n<meta property=\"og:description\" content=\"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link()\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/\" \/>\n<meta property=\"og:site_name\" content=\"\u00ableftbraned\" \/>\n<meta property=\"article:published_time\" content=\"2005-11-01T19:39:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-02-16T01:56:28+00:00\" \/>\n<meta name=\"author\" content=\"Ron\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ron\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/\",\"url\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/\",\"name\":\"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link() - \u00ableftbraned\",\"isPartOf\":{\"@id\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/#website\"},\"datePublished\":\"2005-11-01T19:39:30+00:00\",\"dateModified\":\"2019-02-16T01:56:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/#\/schema\/person\/86056901135a054d2fa85dcd1f43555a\"},\"description\":\"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link()\",\"breadcrumb\":{\"@id\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/#website\",\"url\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/\",\"name\":\"\u00ableftbraned\",\"description\":\"go. figure. \u00ab\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/#\/schema\/person\/86056901135a054d2fa85dcd1f43555a\",\"name\":\"Ron\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a060c4a10d3c4fecd6555ad6b7e9c08d?s=96&d=identicon&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a060c4a10d3c4fecd6555ad6b7e9c08d?s=96&d=identicon&r=pg\",\"caption\":\"Ron\"},\"description\":\"https:\/\/www.ronrothman.com\/public\/about+me.shtml\",\"sameAs\":[\"https:\/\/www.ronrothman.com\/\"],\"url\":\"https:\/\/www.ronrothman.com\/public\/leftbraned\/author\/ron\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link() - \u00ableftbraned","description":"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link()","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:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/","og_locale":"en_US","og_type":"article","og_title":"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link() - \u00ableftbraned","og_description":"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link()","og_url":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/","og_site_name":"\u00ableftbraned","article_published_time":"2005-11-01T19:39:30+00:00","article_modified_time":"2019-02-16T01:56:28+00:00","author":"Ron","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Ron","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/","url":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/","name":"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link() - \u00ableftbraned","isPartOf":{"@id":"https:\/\/www.ronrothman.com\/public\/leftbraned\/#website"},"datePublished":"2005-11-01T19:39:30+00:00","dateModified":"2019-02-16T01:56:28+00:00","author":{"@id":"https:\/\/www.ronrothman.com\/public\/leftbraned\/#\/schema\/person\/86056901135a054d2fa85dcd1f43555a"},"description":"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link()","breadcrumb":{"@id":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wordpress-fix-category-exclusions-in-next_post_linkprevious_post_link\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.ronrothman.com\/public\/leftbraned\/"},{"@type":"ListItem","position":2,"name":"WordPress: Fix Category Exclusions in next_post_link() \/ previous_post_link()"}]},{"@type":"WebSite","@id":"https:\/\/www.ronrothman.com\/public\/leftbraned\/#website","url":"https:\/\/www.ronrothman.com\/public\/leftbraned\/","name":"\u00ableftbraned","description":"go. figure. \u00ab","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.ronrothman.com\/public\/leftbraned\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.ronrothman.com\/public\/leftbraned\/#\/schema\/person\/86056901135a054d2fa85dcd1f43555a","name":"Ron","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.ronrothman.com\/public\/leftbraned\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a060c4a10d3c4fecd6555ad6b7e9c08d?s=96&d=identicon&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a060c4a10d3c4fecd6555ad6b7e9c08d?s=96&d=identicon&r=pg","caption":"Ron"},"description":"https:\/\/www.ronrothman.com\/public\/about+me.shtml","sameAs":["https:\/\/www.ronrothman.com\/"],"url":"https:\/\/www.ronrothman.com\/public\/leftbraned\/author\/ron\/"}]}},"_links":{"self":[{"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/posts\/141"}],"collection":[{"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/comments?post=141"}],"version-history":[{"count":2,"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":531,"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/posts\/141\/revisions\/531"}],"wp:attachment":[{"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronrothman.com\/public\/leftbraned\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}