{"id":6704,"date":"2022-07-17T10:55:32","date_gmt":"2022-07-17T01:55:32","guid":{"rendered":"https:\/\/the-world-star.com\/?p=6704"},"modified":"2026-11-21T11:46:26","modified_gmt":"2026-11-21T02:46:26","slug":"how-to-limit-previous-and-next-posts-to-the-same-category-in-wordpress-lightning","status":"publish","type":"post","link":"https:\/\/the-world-star.com\/en\/server\/how-to-limit-previous-and-next-posts-to-the-same-category-in-wordpress-lightning-6704\/","title":{"rendered":"How to Limit \"Previous\" and \"Next\" Posts to the Same Category in WordPress Lightning"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What I Wanted to Achieve<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I wanted to restrict the &quot;Previous Post&quot; \/ &quot;Next Post&quot; links that appear at the bottom of blog posts in the WordPress Lightning theme so that they only show posts from the same category.<br>As a blogger with diverse hobbies, I write about a wide variety of topics. If someone is reading a page about &quot;Fishing,&quot; they probably won&#039;t be interested if the &quot;Next Post&quot; happens to be something completely unrelated like &quot;Server Setup.&quot; They want to see the next fishing article!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Initially, I even considered and tested setting up WordPress multisite so the topics could live on entirely separate sites. However, I ran into issues with incompatible plugins, and since it seemed unstable, I gave up\u2014though reverting it required deleting the database, which was a pain.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized is-style-vk-image-shadow\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17105052\/next_different_category.png\" alt=\"\" class=\"wp-image-1958\" width=\"674\" height=\"306\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17105052\/next_different_category.png 814w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17105052\/next_different_category-300x136.png 300w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17105052\/next_different_category-768x349.png 768w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I did some research, but most of the information seemed outdated, and I couldn&#039;t find a definitive solution. Since I&#039;m using the Lightning theme, I searched for solutions specific to it, but those guides were old too. I also tried figuring it out by looking at PHP code. While I don&#039;t know PHP very well, it&#039;s a C-style derivative language, so I could generally read it. As I picked up various pieces of knowledge along the way, I decided to take notes on the process.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion: Edit the Lightning &#039;next-prev.php&#039; file and change &quot;$in_same_term&quot; to &quot;true&quot;<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For those who just want the answer without reading about the trial-and-error process, here is the conclusion.<br>Here is how to make the &quot;Previous Post&quot; and &quot;Next Post&quot; links in Lightning display only posts from the exact same category:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/var\/www\/html\/wp-content\/themes\/lightning\/_g3\/template-parts\/next-prev.php<br>Open this file in an editor, edit it as shown below, and save it. You&#039;re just changing one line from false to true.<br>(Please look elsewhere if you need to learn how to SSH into a basic server and edit with sudo as root, or how to use FTP if you can&#039;t do that.)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nglobal $bootstrap;\n\/\/$in_same_term   = apply_filters( 'lightning_prev_next_post_in_same_term', <span data-color=\"#fcb900\" style=\"background: linear-gradient(transparent 60%,rgba(252, 185, 0, 0.7) 0);\" class=\"vk_highlighter\">false<\/span> );\n<span data-color=\"#8ed1fc\" style=\"background: linear-gradient(transparent 60%,rgba(142, 209, 252, 0.7) 0);\" class=\"vk_highlighter\">$in_same_term<\/span>   = apply_filters( 'lightning_prev_next_post_in_same_term', <span data-color=\"#cf2e2e\" style=\"background: linear-gradient(transparent 60%,rgba(207, 46, 46, 0.7) 0);\" class=\"vk_highlighter\">true<\/span> ); \/\/\u3053\u3053\u304b\u306a\uff1f\n$excluded_terms = apply_filters( 'lightning_prev_next_post_excluded_terms', '' );\n$taxonomy       = apply_filters( 'lightning_prev_next_post_taxonomy', 'category' );\n?&gt;\n\n&lt;?php\n$post_previous = get_previous_post( <span data-color=\"#8ed1fc\" style=\"background: linear-gradient(transparent 60%,rgba(142, 209, 252, 0.7) 0);\" class=\"vk_highlighter\">$in_same_ter<\/span>m, $excluded_terms, $taxonomy );\n$post_next     = get_next_post( <span data-color=\"#8ed1fc\" style=\"background: linear-gradient(transparent 60%,rgba(142, 209, 252, 0.7) 0);\" class=\"vk_highlighter\">$in_same_term<\/span>, $excluded_terms, $taxonomy );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Around the third line from the top, where the arguments for get_previous_post() and get_next_post() are defined:<span data-color=\"#8ed1fc\" style=\"background: linear-gradient(transparent 60%,rgba(142, 209, 252, 0.7) 0);\" class=\"vk_highlighter\">$in_same_term<\/span>Change this from false to true.<br>If you reload your post, you should see that it worked! The final results are at the very bottom, but feel free to read through the investigation process below as well.<br><br>Since I&#039;m directly editing a Lightning theme file, the changes will likely be overwritten when Lightning updates (which happens quite frequently). I&#039;ll probably need to re-apply this fix after every update. I should definitely make a backup copy of this edited next-prev.php file. I really wish WordPress offered this as a customizable setting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lightning has different generations, and as of writing this article, it is on Generation3. Even if the generation changes in the future, a similar file should exist under the corresponding _g4 directory. Future readers, please use the investigation notes below to track it down.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Investigation Process<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What I Learned Initially: &quot;next_post&quot; is the Keyword<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/sole-color-blog.com\/blog\/57\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/sole-color-blog.com\/blog\/57\/<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u540c\u3058\u30ab\u30c6\u30b4\u30ea\u306b\u7d5e\u3063\u305f\u30da\u30fc\u30b8\u30ea\u30f3\u30af\u3092\u4ed8\u3051\u308b\u306b\u306f\u3001\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u62ec\u5f27\uff08\uff09\u306e\u4e2d\u3078\u5f15\u6570\u3092\u30bb\u30c3\u30c8\u3057\u307e\u3059\u3002<br>\u30dd\u30a4\u30f3\u30c8\u306f\u3001\u7b2c3\u5f15\u6570\u306e\u300ctrue\u300d\u3067\u3001\u3053\u306e\u7b2c3\u5f15\u6570\u3092true\u306b\u30bb\u30c3\u30c8\u3059\u308b\u3053\u3068\u3067\u3001\u540c\u3058\u30ab\u30c6\u30b4\u30ea\u306b\u7d5e\u3063\u305f\u30da\u30fc\u30b8\u30ea\u30f3\u30af\u3092\u4ed8\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p><cite>https:\/\/sole-color-blog.com\/blog\/57\/<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php previous_post_link('&amp;laquo; %link', '%title', true , ''); ?&gt;\n&lt;?php next_post_link('%link &amp;raquo;', '%title', true , ''); ?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Setting the third argument in this function to true seems to restrict the &quot;Next&quot; post to the same category.<br>However, it didn&#039;t specify *where* to write this code.<br>That kind of advice is probably intended for people building themes from scratch who are expected to already know these things.<br>Searching for next_post_link brings up official-looking documentation that explains exactly that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">_link<a href=\"https:\/\/wpdocs.osdn.jp\/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0\/previous_post_link\" data-type=\"URL\" data-id=\"https:\/\/wpdocs.osdn.jp\/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0\/previous_post_link\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/wpdocs.osdn.jp\/%E3%83%86%E3%83%B3%E3%83%97%E3%83%99%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0\/previous_post_link<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Searching for next_post_link()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>grep next_post_link -rl .<br>nynt@X28k:\/var\/www\/html\/$ grep next_post_link -rl .<br>..\/wp-includes\/link-template.php<br>..\/wp-includes\/deprecated.php<br>..\/wp-content\/themes\/lightning\/_g2\/template-parts\/post\/next-prev.php<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By the way, around this time I wanted a testing environment, so I set up Apache2 and a WordPress environment on my local PC. Thanks to that, I now have a dedicated testing and experimentation sandbox.<br>It took me a little while to figure out (process omitted), but for Lightning, while it seems to have been used in __g2 = G2 = Generation 2, it is no longer used in the current Generation 3.<br>The wp-includes directory handles default WordPress processes, while the wp-content\/themes\/lightning\/ directory contains the PHP files for the installed Lightning theme.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u25a0\/lightning\/_g2\/template-parts\/post\/next-prev.php ... This is for g2, so it&#039;s not being used.<br>\u25a0..\/wp-includes\/deprecated.php: Called from next_post(). Marked as deprecated. Is it deprecated? Is it unused?<br>\u25a0..\/wp-includes\/link-template.php ... Function definitions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tracing it back didn&#039;t lead anywhere, so it appears next_post_link() isn&#039;t actually being used (though I didn&#039;t realize this at the time).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Casting a Wider Net 1: Grepping for next_post<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>nynt@X28k:\/var\/www\/html$ grep next_post -rl ..\/\n..\/html\/wp-includes\/link-template.php\n..\/html\/wp-includes\/deprecated.php\n..\/html\/wp-includes\/blocks\/query-pagination-next.php\n..\/html\/wp-includes\/blocks\/comments-pagination-next.php\n..\/html\/wp-includes\/blocks\/template-part.php\n..\/html\/wp-includes\/class-wp-query.php\n..\/html\/wp-includes\/link-template.php~\n..\/html\/wp-admin\/includes\/nav-menu.php\n..\/html\/wp-admin\/includes\/export.php\n..\/html\/wp-content\/themes\/lightning\/_g3\/template-parts\/next-prev.php\n..\/html\/wp-content\/themes\/lightning\/_g2\/template-parts\/post\/next-prev.php\n..\/html\/wp-content\/themes\/twentytwenty\/inc\/template-tags.php\n..\/html\/wp-content\/themes\/twentytwenty\/template-parts\/navigation.php<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I dropped &quot;link&quot; from the search to cast a wider net. A ton of results came up, including\u2014ah, yes\u2014the version for __g3 inside lightning\/_g2&#039;s next-prev.php.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Casting a Wider Net 2: Grepping for next-prev<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ynt@X28k:\/var\/www\/html$ grep next-prev -rl .\n.\/wp-content\/themes\/lightning\/_g3\/assets\/_scss\/style.scss\n.\/wp-content\/themes\/lightning\/_g3\/assets\/_scss\/project\/_next-prev.scss\n.\/wp-content\/themes\/lightning\/_g3\/assets\/css\/style.css\n.\/wp-content\/themes\/lightning\/_g3\/template-parts\/next-prev.php\n.\/wp-content\/themes\/lightning\/_g3\/template-parts\/main-singular.php\n.\/wp-content\/themes\/lightning\/_g2\/template-parts\/post\/content.php\n.\/wp-content\/languages\/themes\/lightning-ja.po<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Could this be it?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A lot of files related to Lightning popped up, specifically for G3, which I&#039;m currently using. Stylesheets (=css) appeared too, so the code is likely being called from around here.<br>Here is my hypothesis:<br>scss\/style.css calls next-prev.scss (via the statement @import &quot;project\/next-prev&quot;;)<br>_scss\/project\/_next-prev.scss defines &quot;.next-prev {&quot;, but it only contains entries like &quot;vk~&quot; which look like extension plugin code?<br>\/style.css has no line breaks and is unreadable, but since it contains the &quot;.next-prev&quot; rule mentioned above, that&#039;s what gets invoked.<br>Does the filename \/template-parts\/next-prev.php itself act as a function name? It uses get_next_post(), which contains the actual function implementation.<br>Since this was also used in the original next_post_link, I can deduce that this has replaced the old method and is what&#039;s currently in use. Because it passes &quot;same category = false&quot; as an argument, I couldn&#039;t resist and decided to modify it right here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Modifying .\/wp-content\/themes\/lightning\/_g3\/template-parts\/next-prev.php<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nglobal $bootstrap;\n\/\/$in_same_term   = apply_filters( 'lightning_prev_next_post_in_same_term', <span data-color=\"#fcb900\" style=\"background: linear-gradient(transparent 60%,rgba(252, 185, 0, 0.7) 0);\" class=\"vk_highlighter\">false<\/span> );\n<span data-color=\"#8ed1fc\" style=\"background: linear-gradient(transparent 60%,rgba(142, 209, 252, 0.7) 0);\" class=\"vk_highlighter\">$in_same_term<\/span>   = apply_filters( 'lightning_prev_next_post_in_same_term', <span data-color=\"#cf2e2e\" style=\"background: linear-gradient(transparent 60%,rgba(207, 46, 46, 0.7) 0);\" class=\"vk_highlighter\">true<\/span> ); \/\/\u3053\u3053\u304b\u306a\uff1f\n$excluded_terms = apply_filters( 'lightning_prev_next_post_excluded_terms', '' );\n$taxonomy       = apply_filters( 'lightning_prev_next_post_taxonomy', 'category' );\n?&gt;\n\n&lt;?php\n$post_previous = get_previous_post( <span data-color=\"#8ed1fc\" style=\"background: linear-gradient(transparent 60%,rgba(142, 209, 252, 0.7) 0);\" class=\"vk_highlighter\">$in_same_ter<\/span>m, $excluded_terms, $taxonomy );\n$post_next     = get_next_post( <span data-color=\"#8ed1fc\" style=\"background: linear-gradient(transparent 60%,rgba(142, 209, 252, 0.7) 0);\" class=\"vk_highlighter\">$in_same_term<\/span>, $excluded_terms, $taxonomy );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The details are exactly as I wrote at the beginning. It&#039;s just as it looks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">It Worked!<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17101422\/Screenshot-from-2022-07-17-10-13-34.png\" alt=\"\" class=\"wp-image-1951\" width=\"573\" height=\"333\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17101422\/Screenshot-from-2022-07-17-10-13-34.png 908w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17101422\/Screenshot-from-2022-07-17-10-13-34-300x174.png 300w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17101422\/Screenshot-from-2022-07-17-10-13-34-768x446.png 768w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"968\" height=\"315\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17101600\/Screenshot-from-2022-07-17-10-15-24.png\" alt=\"\" class=\"wp-image-1952\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17101600\/Screenshot-from-2022-07-17-10-15-24.png 968w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17101600\/Screenshot-from-2022-07-17-10-15-24-300x98.png 300w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/17101600\/Screenshot-from-2022-07-17-10-15-24-768x250.png 768w\" sizes=\"auto, (max-width: 968px) 100vw, 968px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The post I was currently viewing belongs to the &quot;Camping&quot; category: &quot;Fishing &amp; Camping Hirado Ikitsuki Island - Camping Edition -&quot;. Previously, the surrounding posts would have been unrelated kayak fishing articles, but now it correctly skips those and shows other &quot;Camping&quot; posts like &quot;Bonfire Stands&quot; and &quot;Umi Town Showa no Mori Campground&quot; instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It took a bit of effort and I relied on other sites to get started, but solving it mostly on my own feels great. If no one else has posted a solution to this out there, hopefully, this article will be helpful to others. Or maybe my search skills are just bad? Let&#039;s try searching again with the keywords I&#039;ve learned this time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, I want to do the same thing for the &quot;Recent Posts&quot; widget\u2014limiting them to the same category, or maybe by parent category... Sounds tough though.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What I Wanted to Achieve I wanted to restrict the &quot;Previous Post&quot; \/ &quot;Next Post&quot; links that appear at the bottom of blog posts in the WordPress Lightning theme so that they only show posts from the same category.As a blogger with diverse hobbies, I write about a wide variety of topics. If someone is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1958,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/175.41.248.22\/?p=1943","_lightning_design_setting":{"layout":"default"},"footnotes":""},"categories":[3],"tags":[78,79,1259,67,77],"class_list":["post-6704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-previous_post","tag-next-prev","tag-same_category","tag-server","tag-next_post","en-US"],"_links":{"self":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/comments?post=6704"}],"version-history":[{"count":1,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6704\/revisions"}],"predecessor-version":[{"id":6705,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6704\/revisions\/6705"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media\/1958"}],"wp:attachment":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media?parent=6704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/categories?post=6704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/tags?post=6704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}