Archived topic
Exclude categories from post navigation
9 replies · Started by Bill on January 14, 2023
I have core content in a range of topics that I want included in the post navigation. No problem there.
I have one category that I want excluded from the post navigation.
I couldn't see a filter available to do this, so I looked at the underlying theme code. It seemed like I could create a filter hook and then add a code snippet. That didn't seem to work.
But it looked like this:
add_filter( 'generate_post_navigation_exclusion_terms', function() {
return '13';
} );
I've also tried this:
add_filter( 'generate_post_navigation_args', function( $args ) {
if ( is_single() ) {
$args['excluded_terms'] = '13';
}
return $args;
} );
Each time, I still see a link to the post in that category.
Is there a simpler and reliable way to achieve the results I'm looking for?
As I look more at this, I am using the Ultimate Category Excluder where I exclude the category from the Front Page.
The default navigation works properly.
The post navigation element doesn't work properly and I wonder if it's possible there is a bug. In theory, it's the same query that's being modified by the plugin.
Hi Bill,
Try this solution:
https://generatepress.com/forums/topic/post-navigation-how-exclude-a-category-from-next-and-previous-post-links/page/2/#post-2247795
Replace the 29 with the category ID you want to exclude.
Thanks for the link.
I write code for a living, so I'm not afraid of coding. But that is too much code to have to add to accomplish this task. I should be able to add a filter (if one doesn't exist) and then add a function for that filter.
I should not have to replace entire functions as that solution prescribes. If that function takes on more responsibilities over time or changes its interface, then I will potentially have problems.
I think generatepress is missing a useful filter and/or has a bug in the post navigation implementation.
I think generatepress is missing a useful filter and/or has a bug in the post navigation implementation.
There was a WP bug so the below code could not work, can you give it another try?
add_filter( 'get_next_post_excluded_terms', 'tu_exclude_terms' );
add_filter( 'get_previous_post_excluded_terms', 'tu_exclude_terms' );
function tu_exclude_terms() {
return array( '29' );
}
It seems the filter assumes the list of id's is an array, so you do not have to declare it. This works for me:
add_filter( 'get_next_post_excluded_terms', 'tu_exclude_terms' );
add_filter( 'get_previous_post_excluded_terms', 'tu_exclude_terms' );
function tu_exclude_terms() {
return '304,30169';
}
Hi, I would like to use this filter too. Can you please explain how I can add it? Should I use the wp_head in the Hooks Element?
Thank you!
Eva
Hi Eva,
that code is PHP, and it cannot be added to a GP Element.
This article explains how to add the code:
Thank you, David. I just wanted to be sure and will use the Code Snippets plugin then.
Glad to be of help!