Skip to content

Instantly share code, notes, and snippets.

@kovshenin
Created October 4, 2012 13:19
Show Gist options
  • Save kovshenin/3833482 to your computer and use it in GitHub Desktop.
Save kovshenin/3833482 to your computer and use it in GitHub Desktop.
Remove posts from a certain category
<?php
/**
* Plugin Name: Hide posts from category on home page
*/
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $query ) {
// Category IDs to exclude
$exclude_categories = array( 1, 2, 3 );
if ( $query->is_home() )
$query->set( 'category__not_in', $exclude_categories );
}
@byjml
Copy link

byjml commented Oct 4, 2012

...and when people want to display a portion of their blog posts on their static home page, they can expand the $query->is_home() condition like this:

if ( $query->is_home() || $query->is_front_page() )
    ...

It seems that many WP developers use query_posts() which, in fact, causes more problems than it solves.

Sweet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment