Hide Specific Posts From the Blog Page
Keeps the posts you list off the main blog page and its pagination while they stay reachable through their own URL, categories and search.
Opens WPIDE on
Code
// IDs of posts to keep off the blog page.
$wpide_hidden_from_blog = array( 0 );
add_action( 'pre_get_posts', function ( $query ) use ( $wpide_hidden_from_blog ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_home() ) {
return;
}
$query->set( 'post__not_in', array_merge( (array) $query->get( 'post__not_in' ), array_map( 'absint', $wpide_hidden_from_blog ) ) );
} );
Notes
Add the post IDs to the list.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.