Php 在wordpress中按标签或类别搜索不带插件的记录

Php 在wordpress中按标签或类别搜索不带插件的记录,php,wordpress,search,tags,categories,Php,Wordpress,Search,Tags,Categories,我正在使用WordPress搜索,它可以根据内容和标题进行搜索。现在我必须按标签或类别搜索产品 那么,有什么办法可以解决这个问题呢 下面是我在search.php上使用的代码 <section id="primary" class="content-area mainSearch"> <main id="main" class="site-main"> <div class="equalPadding"> <div c

我正在使用WordPress搜索,它可以根据内容和标题进行搜索。现在我必须按标签或类别搜索产品

那么,有什么办法可以解决这个问题呢

下面是我在search.php上使用的代码

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

您能帮我一下吗?

对于标签或类别,您可以执行以下代码

$args = array(
    'tax_query' => array(
       'relation' => 'OR',
        array(
            'taxonomy' => 'product_tag',
            'field' => 'slug',
            'terms' => 'search value'
        ),
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'search value'
        )
    ),
    'post_type' => 'product'
);

$products= get_posts( $args );

我需要知道我应该在哪里使用上述答案?我如何在我的代码中使用它?这是获取帖子的查询,所以你可以在获取产品的地方使用它。这里有点混乱。产品代码(部分)已添加到问题中。我是否应该删除while(have_posts()){The_post();条件?我应该在这个“分类法”=>“产品标签”、“字段”=>“slug”、“术语”=>“搜索值”中使用什么?
$args = array(
    'tax_query' => array(
       'relation' => 'OR',
        array(
            'taxonomy' => 'product_tag',
            'field' => 'slug',
            'terms' => 'search value'
        ),
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'search value'
        )
    ),
    'post_type' => 'product'
);

$products= get_posts( $args );