How to avoid the specific page while searching in WordPress without plugin?

I am using WordPress search which is working but I am getting one issue on the search result.

My issue is, when I search my product then I am getting the output but it also displaying the privacy policy results.

Example

enter image description here

Displaying result code

<section id="primary" class="content-area mainSearch">
    <main id="main" class="site-main">
      <div class="equalPadding">
        <div class="cp-seeWrapper">
    <?php 
          if ( have_posts() ) : 
          get_search_form();//search form
          ?>
         <div class="row">
      <?php 

       while (have_posts()){ the_post();?>
     <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12 ">
        <div class="cp-shadow cp-seeSinglePostWrapper">
        <?php the_post_thumbnail();?>
          <div class="bg-white single-post-box">
          <div class="d-flex cp-CategoryList">
           <div class="seeDate"><?php echo get_the_date('F j, Y'); ?></div>
            <div class="cp_cat_list">
              <?php $cat = get_the_category();?>
              <a href="https://wordpress.stackexchange.com/questions/353821/<?php echo esc_url( get_category_link( $cat[0]->term_id ) );?>"><?php echo $cat[0]->cat_name?></a><?php ?>
            </div>
          </div>
            <div class="cp-b-content"><h2><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>"><?php echo wp_trim_words(get_the_title(), 12, '...'); ?></a></h2></div>
            <p><?php echo wp_trim_words(get_the_excerpt(), 25, '...'); ?></p>
        </div>
        </div>
        </div>
       <?php } ?>

      <?php

      //the_posts_navigation();

    else :
      get_template_part( 'template-parts/content', 'none' );

    endif;
    ?>
          </div>
    </div>
    </div>
    </main><!-- #main -->
  </section><!-- #primary -->

I want to displaying only my post result but it also displaying the page. Would you help me out?

Custom post type

// Register Custom Post Type testingpost
function create_testingpost_cpt() {

  $labels = array(
    'name' => _x( 'testingposts', 'Post Type General Name', 'textdomain' ),
    'singular_name' => _x( 'testingpost', 'Post Type Singular Name', 'textdomain' ),
    'menu_name' => _x( 'testingposts', 'Admin Menu text', 'textdomain' ),
    'name_admin_bar' => _x( 'testingpost', 'Add New on Toolbar', 'textdomain' ),
    'archives' => __( 'testingpost Archives', 'textdomain' ),
    'attributes' => __( 'testingpost Attributes', 'textdomain' ),
    'parent_item_colon' => __( 'Parent testingpost:', 'textdomain' ),
    'all_items' => __( 'All testingposts', 'textdomain' ),
    'add_new_item' => __( 'Add New testingpost', 'textdomain' ),
    'add_new' => __( 'Add New', 'textdomain' ),
    'new_item' => __( 'New testingpost', 'textdomain' ),
    'edit_item' => __( 'Edit testingpost', 'textdomain' ),
    'update_item' => __( 'Update testingpost', 'textdomain' ),
    'view_item' => __( 'View testingpost', 'textdomain' ),
    'view_items' => __( 'View testingposts', 'textdomain' ),
    'search_items' => __( 'Search testingpost', 'textdomain' ),
    'not_found' => __( 'Not found', 'textdomain' ),
    'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
    'featured_image' => __( 'Featured Image', 'textdomain' ),
    'set_featured_image' => __( 'Set featured image', 'textdomain' ),
    'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
    'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
    'insert_into_item' => __( 'Insert into testingpost', 'textdomain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this testingpost', 'textdomain' ),
    'items_list' => __( 'testingposts list', 'textdomain' ),
    'items_list_navigation' => __( 'testingposts list navigation', 'textdomain' ),
    'filter_items_list' => __( 'Filter testingposts list', 'textdomain' ),
  );
  $args = array(
    'label' => __( 'testingpost', 'textdomain' ),
    'description' => __( '', 'textdomain' ),
    'labels' => $labels,
    'menu_icon' => '',
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'author'),
    'taxonomies' => array('category'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'can_export' => true,
    'has_archive' => true,
    'hierarchical' => false,
    'exclude_from_search' => false,
    'show_in_rest' => true,
    'publicly_queryable' => true,
    'capability_type' => 'post',
  );
  register_post_type( 'testingpost', $args );

}
add_action( 'init', 'create_testingpost_cpt', 0 );

1 Answer
1

What you want to do is use the pre_get_posts action to exclude the post ID from the search results query. This allows you to “pre” set some elements of the query, including an array of post IDs to exclude with the “post__not_in” value.

Suppose the ID of your privacy policy it 11, then you’d pass 11 in the post__not_in array along with any other IDs you are excluding.

add_action( 'pre_get_posts', 'my_search_exclude' );
function my_search_exclude( $query ) {
  if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
    $query->set( 'post_type', array( 'post', 'page', 'product' ) );
    $query->set( 'post__not_in', array( 11 ) );
  }
}

Leave a Comment