Skip to content

Instantly share code, notes, and snippets.

@megantaylor
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megantaylor/fb86fb9cfc026208b928 to your computer and use it in GitHub Desktop.
Save megantaylor/fb86fb9cfc026208b928 to your computer and use it in GitHub Desktop.
exclude categories from WP home page
in functions.php file
<?php
add_filter( 'pre_get_posts', function ( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$example_term = get_term_by( 'name', 'featured', 'category' );
$example_term_2 = get_term_by( 'name', 'Featured Post', 'category' );
$example_term_3 = get_term_by( 'name', '1,000 Stories', 'category' );
$example_term_4 = get_term_by( 'name', '1,000 Stories Spotlight', 'category' );
$query->set( 'category__not_in', $example_term->term_id, $example_term_2->term_id, $example_term_3->term_id, $example_term_4->term_id );
}
});
?>
in index.php file
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
x_get_view( 'ethos', 'content', get_post_format() );
}
}
?>
@tomjn
Copy link

tomjn commented Aug 10, 2014

That's not a valid post loop, and a mixture of 2 syntax's. This is what a standard post loop should look like:

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        // do things
    }
}

notes:

  • if: endif; and while: endwhile; are shorthand
  • if() {} and while () {} are not shorthand
  • putting the_post(); on the same line as the while loop is bad practice, but it appears everywhere. A single line should do a single thing, makes it easier to debug
  • you've got stray ?><?php tags in there too, save yourself the time and don't bother typing them out, it does nothing

@megantaylor
Copy link
Author

Thanks! Fixed that, but posts are still not being excluded.

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