1

As you can see, on the second line here, I am effecting the main query only. This would have no effect on custom queries elsewhere on the site. How do you apply it to custom queries too?

function exclude_category( $query ) {
  if ( $query->is_home() && $query->is_main_query() ) {
      $query->set( 'cat', '-9' );
  }
}
add_action('pre_get_posts',  __NAMESPACE__ . '\\exclude_category');
1

It's a custom query. In that case, you can pass the parameter directly to the custom query. Otherwise, you can add other conditions in 'exclude_category' function. for instance, You can check whether category, post type, archive or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.