How to Limit "Previous" and "Next" Posts to the Same Category in WordPress Lightning
What I Wanted to Achieve
I wanted to restrict the "Previous Post" / "Next Post" 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 reading a page about "Fishing," they probably won't be interested if the "Next Post" happens to be something completely unrelated like "Server Setup." They want to see the next fishing article!
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—though reverting it required deleting the database, which was a pain.

I did some research, but most of the information seemed outdated, and I couldn't find a definitive solution. Since I'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't know PHP very well, it'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.
Conclusion: Edit the Lightning 'next-prev.php' file and change "$in_same_term" to "true"
For those who just want the answer without reading about the trial-and-error process, here is the conclusion.
Here is how to make the "Previous Post" and "Next Post" links in Lightning display only posts from the exact same category:
/var/www/html/wp-content/themes/lightning/_g3/template-parts/next-prev.php
Open this file in an editor, edit it as shown below, and save it. You're just changing one line from false to true.
(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't do that.)
<?php
global $bootstrap;
//$in_same_term = apply_filters( 'lightning_prev_next_post_in_same_term', false );
$in_same_term = apply_filters( 'lightning_prev_next_post_in_same_term', true ); //ここかな?
$excluded_terms = apply_filters( 'lightning_prev_next_post_excluded_terms', '' );
$taxonomy = apply_filters( 'lightning_prev_next_post_taxonomy', 'category' );
?>
<?php
$post_previous = get_previous_post( $in_same_term, $excluded_terms, $taxonomy );
$post_next = get_next_post( $in_same_term, $excluded_terms, $taxonomy );
Around the third line from the top, where the arguments for get_previous_post() and get_next_post() are defined:$in_same_termChange this from false to true.
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.
Since I'm directly editing a Lightning theme file, the changes will likely be overwritten when Lightning updates (which happens quite frequently). I'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.
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.
The Investigation Process
What I Learned Initially: "next_post" is the Keyword
https://sole-color-blog.com/blog/57/
同じカテゴリに絞ったページリンクを付けるには、上記コードの括弧()の中へ引数をセットします。
https://sole-color-blog.com/blog/57/
ポイントは、第3引数の「true」で、この第3引数をtrueにセットすることで、同じカテゴリに絞ったページリンクを付けることができるようになります。
<?php previous_post_link('« %link', '%title', true , ''); ?>
<?php next_post_link('%link »', '%title', true , ''); ?>
Setting the third argument in this function to true seems to restrict the "Next" post to the same category.
However, it didn't specify *where* to write this code.
That kind of advice is probably intended for people building themes from scratch who are expected to already know these things.
Searching for next_post_link brings up official-looking documentation that explains exactly that.
Searching for next_post_link()
grep next_post_link -rl .
nynt@X28k:/var/www/html/$ grep next_post_link -rl .
../wp-includes/link-template.php
../wp-includes/deprecated.php
../wp-content/themes/lightning/_g2/template-parts/post/next-prev.php
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.
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.
The wp-includes directory handles default WordPress processes, while the wp-content/themes/lightning/ directory contains the PHP files for the installed Lightning theme.
■/lightning/_g2/template-parts/post/next-prev.php ... This is for g2, so it's not being used.
■../wp-includes/deprecated.php: Called from next_post(). Marked as deprecated. Is it deprecated? Is it unused?
■../wp-includes/link-template.php ... Function definitions.
Tracing it back didn't lead anywhere, so it appears next_post_link() isn't actually being used (though I didn't realize this at the time).
Casting a Wider Net 1: Grepping for next_post
nynt@X28k:/var/www/html$ grep next_post -rl ../
../html/wp-includes/link-template.php
../html/wp-includes/deprecated.php
../html/wp-includes/blocks/query-pagination-next.php
../html/wp-includes/blocks/comments-pagination-next.php
../html/wp-includes/blocks/template-part.php
../html/wp-includes/class-wp-query.php
../html/wp-includes/link-template.php~
../html/wp-admin/includes/nav-menu.php
../html/wp-admin/includes/export.php
../html/wp-content/themes/lightning/_g3/template-parts/next-prev.php
../html/wp-content/themes/lightning/_g2/template-parts/post/next-prev.php
../html/wp-content/themes/twentytwenty/inc/template-tags.php
../html/wp-content/themes/twentytwenty/template-parts/navigation.php
I dropped "link" from the search to cast a wider net. A ton of results came up, including—ah, yes—the version for __g3 inside lightning/_g2's next-prev.php.
Casting a Wider Net 2: Grepping for next-prev
ynt@X28k:/var/www/html$ grep next-prev -rl .
./wp-content/themes/lightning/_g3/assets/_scss/style.scss
./wp-content/themes/lightning/_g3/assets/_scss/project/_next-prev.scss
./wp-content/themes/lightning/_g3/assets/css/style.css
./wp-content/themes/lightning/_g3/template-parts/next-prev.php
./wp-content/themes/lightning/_g3/template-parts/main-singular.php
./wp-content/themes/lightning/_g2/template-parts/post/content.php
./wp-content/languages/themes/lightning-ja.po
Could this be it?
A lot of files related to Lightning popped up, specifically for G3, which I'm currently using. Stylesheets (=css) appeared too, so the code is likely being called from around here.
Here is my hypothesis:
scss/style.css calls next-prev.scss (via the statement @import "project/next-prev";)
_scss/project/_next-prev.scss defines ".next-prev {", but it only contains entries like "vk~" which look like extension plugin code?
/style.css has no line breaks and is unreadable, but since it contains the ".next-prev" rule mentioned above, that's what gets invoked.
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.
Since this was also used in the original next_post_link, I can deduce that this has replaced the old method and is what's currently in use. Because it passes "same category = false" as an argument, I couldn't resist and decided to modify it right here.
Modifying ./wp-content/themes/lightning/_g3/template-parts/next-prev.php
<?php
global $bootstrap;
//$in_same_term = apply_filters( 'lightning_prev_next_post_in_same_term', false );
$in_same_term = apply_filters( 'lightning_prev_next_post_in_same_term', true ); //ここかな?
$excluded_terms = apply_filters( 'lightning_prev_next_post_excluded_terms', '' );
$taxonomy = apply_filters( 'lightning_prev_next_post_taxonomy', 'category' );
?>
<?php
$post_previous = get_previous_post( $in_same_term, $excluded_terms, $taxonomy );
$post_next = get_next_post( $in_same_term, $excluded_terms, $taxonomy );
The details are exactly as I wrote at the beginning. It's just as it looks.
It Worked!


The post I was currently viewing belongs to the "Camping" category: "Fishing & Camping Hirado Ikitsuki Island - Camping Edition -". Previously, the surrounding posts would have been unrelated kayak fishing articles, but now it correctly skips those and shows other "Camping" posts like "Bonfire Stands" and "Umi Town Showa no Mori Campground" instead.
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's try searching again with the keywords I've learned this time.
Next, I want to do the same thing for the "Recent Posts" widget—limiting them to the same category, or maybe by parent category... Sounds tough though.


