如何在没有插件的WordPress中搜索时避开特定页面?

时间:2019-12-03 作者:Naren Verma

我正在使用WordPress搜索,但我在搜索结果上遇到了一个问题。

我的问题是,当我搜索我的产品时,我会得到输出,但它也会显示隐私政策结果。

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 -->
我只想显示我的帖子结果,但它也会显示页面。你能帮帮我吗?

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 个回复
最合适的回答,由SO网友:butlerblog 整理而成

你要做的是使用the pre_get_posts action 从搜索结果查询中排除帖子ID。这允许您“预先”设置查询的某些元素,包括要使用“post\\u not\\u in”值排除的post id数组。

假设您的隐私策略ID为11,那么您将在post__not_in 数组以及您排除的任何其他ID。

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 ) );
  }
}

相关推荐

Live search by custom tag

实时搜索:$the_query = new WP_Query( array( \'s\' => esc_attr( $_POST[\'keyword\'] ), \'post_type\' => \'custom_type\', \'sentence\' => \'true\' )); if( $the_query->have_posts() ) : while( $the_query->have_posts() ): $the_query->t