Hide a specific category in admin All Posts page (WordPress)

If we look at the official documentation for WP_Query:

https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

Show posts associated with certain categories.

  • cat (int) – use category id.
  • category_name (string) – use category slug.
  • category__and (array) – use category id.
  • category__in (array) – use category id.
  • category__not_in (array) – use category id.

The problem is that cat demands a category ID, not an array of values.

We can also see a more appropriate option of category__not_in, and we can use an actual array, not a comma separated list:

$query->set( 'category__not_in', [13,14,15,16] );

Which leads to the other problem, the condition:

if ( $query->is_main_query() && !is_admin() && $query->is_home() ) {

If you want this to run on the admin, why is this part in the check?

!is_admin() && $query->is_home()

It says if it is not the admin, and we’re on the homepage, do X. But you want the opposite! Remove the is home bit, and reverse the is_admin by removing the !. This part will require beginner level PHP skills to do.

Finally, asking the database to exclude things, leave stuff out, is expensive, and gets excessively expensive as the amount of content grows.

Additionally, you should never hardcode IDs, all it takes is somebody deleting the category and recreating it, a site migration, etc and it’s broken