Let’s say that I have a post that I only want to be shown in the page of the category that it belongs to. The problem is that this post is also visible in the latest posts page.
Is there a way to hide this post from the latest post page and show it only in the page of the category of the post?
3 Answers
function exclude_single_posts_home($query) {
if ($query->is_home() && $query->is_main_query() && !is_admin()) {
$query->set('post__not_in', array(post-id));
}
}
add_action('pre_get_posts', 'exclude_single_posts_home');
Source: pre_get_posts