Archived topic
How to not show a specific Category in the Blog Layout on the Home page?
1 reply · Started by Raj on January 12, 2023
I want to hide a specific category from the front page (home page) blog section. Please help me. I read other similar threats but they were of no use to me, as I don't have much coding knowledge. Can some one please help me with a step by step guide?
Hi there,
this is the PHP Snippet you require:
function exclude_posts_from_home_page( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$term_id = get_cat_ID( 'term-slug' );
$query->set( 'category__not_in', array( $term_id ) );
}
}
add_action( 'pre_get_posts', 'exclude_posts_from_home_page' );
In the code you will see: $term_id = get_cat_ID( 'term-slug' ); change the term-slug to match your term.
Question - are you using a Child Theme?
If Yes, then add the code to your Child Themes functions.php
If NO, then install the Code Snippets plugin, Add New Snippet and add the code there.