This is pretty easy. WP_Query has different argument you can use to exclude a category. The (little) problematic part is how to exclude that category from all loops, being able to still directly querying it.
You can use 'pre_get_post'
action hook, check the 'category_name'
argument of current query and if it’s different from 'street-style'
, exlude it using 'category__not_in'
WP_Query
argument.
If you don’t check for is_main_query()
it will be applied to all queries, main and custom ones.
To stay able to use 'category__not_in'
argument in your custom query, you should also check if that param is setted and if so merge the actual arguemnt with the one escluding ‘street-style’ category.
Maybe code is cleaner than words:
add_action('pre_get_posts', function($query) {
if ( is_admin() ) return;
if ( $query->get('category_name') !== 'street-style' ) {
$not_now = $query->get('category__not_in') ? : array();
$street_style = get_term_by('slug', 'street-style', 'category');
if ( is_object($street_style) && isset($street_style->term_id) ) {
$query->set('category__not_in', array_merge($not_now, $street_style->term_id ) );
}
}
});
Using this code you’ll be able to get posts in ‘street-style’ category only explicitly setting 'category_name'
to ‘street-style’, just like you do OP code.
Note that my code requires PHP 5.3 or newer.