简体   繁体   English

按标签或类别搜索产品不起作用

[英]search the product by tag or category not working

I am working on a search.我正在搜索。 I am trying to search the product by tag or category.我正在尝试按标签或类别搜索产品。 I tried the below code but it's not working.我尝试了下面的代码,但它不起作用。 I am getting我正进入(状态

Nothing Found没有发现

Also, the Below code is working only with title and content.此外,下面的代码仅适用于标题和内容。

Would you help me out with this?你能帮我解决这个问题吗?

<?php
/**
 * The template for displaying search results pages
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package CoolPickings
 */

get_header();
?>

    <section id="primary" class="content-area mainSearch">
        <main id="main" class="site-main">
            <div class="equalPadding">
                <div class="cp-seeWrapper">
        <?php 

                    if ( have_posts() ) : 
                    ?>
           <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 //the_category(''); 

                            ?>
                        </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 //endwhile;

            //the_posts_navigation();

        else :

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

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

<?php
get_sidebar();
get_footer();

Try the following code below.试试下面的代码。 The only difference is using terms as an array.唯一的区别是将terms用作数组。

$getSearch=get_search_query();
$args = array(
    'tax_query' => array(
       'relation' => 'OR',
        array(
            'taxonomy' => 'product_tag',
            'field' => 'slug',
            'terms' => array($getSearch) // Using the terms as an array
        ),
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => array($getSearch) // Using the terms as an array
        )
    ),
    'post_type' => 'product'
);
$the_query = new WP_Query( $args );

// ... functions.php
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( 's', '' );
    }
}

You can find more info here: https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters您可以在此处找到更多信息: https : //developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM