My archive file contains..

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
<a href="<?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.

  • 1
    You need to use filter pre_get_posts. Check this link – Nilambar Sharma Oct 31 '14 at 3:17

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() )

    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.

  • Thank you Robert, can you help how to use this function, i mean where can i put this function ? in functions.php or directly in archive.php and can i remove first 'if' condition, seems its not necessary. – Siva Oct 31 '14 at 3:33
  • In functions.php. NO, do not remove if-else conditions otherwise these posts will not display anywhere on blog. These conditions are making sure that posts do not display in archive pages only. – Robert hue Oct 31 '14 at 3:47
  • Thanks a lot yar, its working great. I mean do we need to use this... if ( is_admin() || ! $query->is_main_query() ) return; ? please don't mind, i am fresh to wordpress that's why i am asking. – Siva Oct 31 '14 at 3:58
  • With that we are skipping the check for admin wordpress query and main wordpress query. So our function should not interfere with them. But it should not affect these, so you can test it by removing. – Robert hue Oct 31 '14 at 4:02
  • @Robert hue Link to the source of the code please. – Brad Dalton Nov 1 '14 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.