generatepress icon indicating copy to clipboard operation
generatepress copied to clipboard

Filter to exclude category terms from post navigation

Open billeisenhauer opened this issue 2 years ago • 2 comments

Description

Problem I'm trying to solve

I have one category that I want to exclude from my core post navigation. At present, AFAIK, I cannot do that.

Suggested Implementation

Can you give me an additional filter to tap into? I think you could call it something like generate_category_post_navigation_excluded_terms and use its value here.

I'm not a Wordpress or PHP developer, but I think theme users would provide category or tag ids array for those categories that should be excluded from the navigation. Even better, would be a UI which keeps me out of the code, but a filter would work.

billeisenhauer avatar Jan 08 '23 19:01 billeisenhauer

@billeisenhauer can you try the filter below:

Change the numbers 1, 2 for the id or ids of the categories you want to exclude.

add_action( 'pre_get_posts', 'exclude_category_from_post_navigation' );

function exclude_category_from_post_navigation( $query ) {
  if ( $query->is_main_query() && is_single() ) {
    $categories_to_exclude = array( 1, 2 );
    $query->set( 'category__not_in', $categories_to_exclude );
  }
}

JeanPaiva avatar Jan 09 '23 14:01 JeanPaiva

@JeanPaiva This doesn't seem to work. I don't know if that function actually modifies the post navigation query.

But also, my suggestion doesn't seem to work when I add the plug-in point for the filter and then add the filter.

billeisenhauer avatar Jan 14 '23 03:01 billeisenhauer