All WordPress snippets
Want your own copy?
Fork this repo and build your own snippet collection. Fork on GitHub →
WordPress
Exclude a Category From the Main WordPress Loop
Hides posts from a specific category ID across the site's default queries — handy for keeping an internal or catch-all category out of public listings.
php
/**
* Exclude a category from all WordPress loops
*/
add_action( 'pre_get_posts', function( $query ) { // anonymous callback
global $wp_query;
// Hard coded category ID, but can be dynamic: esc_attr(get_option('your-cat-id'));
$excluded_cat_id = 25;
// add category ID to existing, avoid overwriting it
$cat[] = $query->get( 'cat' );
$cat[] = "-" . $excluded_cat_id;
$query->set( 'cat', $cat );
}
});
More WordPress snippets
Register and Output a Custom Nav Menu
Adds a new menu location you can assign in Appearance → Menus, shows how to output it in a template, and includes the multi-menu version too.
Define a Reusable Global String
A simple pattern for storing a value — like a phone number or address — in one place and reusing it anywhere in your theme.
Auto-Escape HTML Typed Inside Code Tags
Makes sure any markup typed inside <code> or <tt> tags in the editor gets escaped automatically, so it displays as visible text instead of being rendered as real HTML.