I have used the 'Recent posts' in Two pages and I need to restrict some posts with specific categories from those two pages. I have added the below code in the functions.php and Saved. But the expected result is not coming. Home page (ID - 12) and Poems Page (ID - 8) are those pages I have added the restriction 772 and 152 are the category ID's of those I want to restrict from the mentioned pages.
function excludeCat($query) {
$page = get_query_var('paged', 1);
$exclude = [
12 => '-772',
8 => '-152'
];
if ($query->is_home && isset($exclude[$page])) {
$query->set('cat', $exclude[$page]);
}
return $query;
}
add_filter('pre_get_posts', 'excludeCat');
Please advise on the code