• Hi everyone, I have posts within a certain category that are purposely hidden from search engines, and we don’t want the posts within that category to appear within the sequential previous/next navigation that appears on the other posts.

    I’m using a child theme of Genesis that has a master switch to turn off all previous/next navigation, which is what I’ve done for the interim. But the ideal solution would be to have a function that expressly excludes posts from that category, and then we could safely turn the prev/next nav back on.

    After much searching I’ve not found a ready made function to solve this, so am having to cobble something together, which I’ m not 100% comfortable with, so would appreciate some coaching.

    I have found this code snippet, referencing uncategorized posts. It was written for Divi (found through a Reddit forum here), but could it work for any theme, provided I swap out the slug name for my category name?

    This is for a very large, live site, so I’m reluctant to chuck random code off the internet before getting some counsel first. Or perhaps there is a better way entirely of writing a function that would serve the purpose.

    Thanks for any insights.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The exact solution depends on what function your theme uses to generate the prev/next links. But in general the solution would alter the respective query to exclude posts in a certain category. The “pre_get_posts” action hook can be used to alter general posts queries. But if your prev/next functions directly query the DB, they’d need to be altered through other means.

    If using pre_get_posts, first verify you’re altering the correct query. Then set the “category__not_in” query var to the ID of the category term whose posts you wish to exclude.

    Thread Starter 2mooses

    (@2mooses)

    Thanks @bcworkz. I tried a test version of the above snippet on a test site with a test category, and no results. My skills are not such that I can write that kind of function from scratch.

    Moderator bcworkz

    (@bcworkz)

    Your snippet looks OK as far as it goes. It’s more focused on archive page content than nav links. It may be that your theme is managing links some other way. You’d need to dig into its source code to determine what it is doing.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.