Here’s my question and ChatGPT’s answer:
User:
write code that filters out post categories on my main page on my wordpress web site and tell me where to put it
AI:
Hello! I’d be glad to help you with that. Here’s the code snippet that you can use to filter out specific post categories on your main page in WordPress:
function exclude_category_home( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( ‘category__not_in’, array( 1, 2, 3 ) ); // Replace 1, 2, 3 with the category IDs you want to exclude
}
}
add_action( ‘pre_get_posts’, ‘exclude_category_home’ );
You can add this code to your theme’s functions.php file. To access it, go to Appearance > Editor from your WordPress dashboard, and look for the functions.php file on the right sidebar.
Just copy and paste the code snippet at the bottom of the file, save it, and you’re good to go! The specified categories will no longer appear on your main page.
I hope that helps! Let me know if you have any other questions.
—
I have the chat module on my site if you want to try it. Feel free to link to it on your site
[ad_2]
You should put it in a child theme so you don’t lose it when your theme gets an update.