My archive file contains..
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a href="https://wordpress.stackexchange.com/questions/167032/<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
But i want to exclude few posts with their ids, how can i do that ?
Important! But those excluded posts should display in the Google search as normal, i don’t want to loss them in the search engines. Just i want to hide in my site only.
Thank you friends.
You can exclude posts from archive pages with thee help of pre_get_posts
action. Usually pre_get_posts
is used to modify main query, so it’s best solution for your problem.
function my_custom_get_posts( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( $query->is_archive() ) {
$query->set( 'post__not_in', array( 7, 11, 21 ) );
}
}
add_action( 'pre_get_posts', 'my_custom_get_posts', 1 );
Please notice 7, 11, 21
in above code, these are the post IDs we are excluding from archive pages. You can specify as many comma separated post IDs as you want.
Yes, it will not prevent Google to index them.