tomusborne / generatepress

453 stars 89 forks source link

Filter to exclude category terms from post navigation #495

Open billeisenhauer opened 1 year ago

billeisenhauer commented 1 year ago

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.

JeanPaiva commented 1 year ago

@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 );
  }
}
billeisenhauer commented 1 year ago

@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.