2

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="<?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

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 ) );
  }
}
  • Yes, It's working. I have one more issue is, I have created a Custom Post Type which is used for the slider. This result is also displaying. so is it possible to hide whole custom post type? – Naren Verma Dec 3 '19 at 13:07
  • You can't specifically exclude a post type. However, you can set which post types are included. This value, like the post__not_in value, is an array. I edited to include this set; you can adjust the array of post types accordingly based on what you want displayed in your results. – butlerblog Dec 3 '19 at 13:14
  • Is it possible to exclude the post_type? for example, I have URL example.com/wp-admin/post-new.php?post_type=testingposts and my post type is testingposts. – Naren Verma Dec 3 '19 at 13:25
  • From my comment above: "You can't specifically exclude a post type. However, you can set which post types are included." Anything not included is thereby excluded. – butlerblog Dec 3 '19 at 13:39
  • Thanks for the help, Yes, It's working perfeclty. upvote from my side. – Naren Verma Dec 3 '19 at 13:40

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.