首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按标签或类别搜索不起作用的产品

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

Stack Overflow用户
提问于 2019-12-12 08:39:18
回答 1查看 88关注 0票数 0

我正在搜索。我试图通过标签或类别搜索产品。我尝试了下面的代码,但它不起作用。我得到了

什么也没找到

另外,下面的代码只适用于标题和内容。

你能帮我解决这个问题吗?

代码语言:javascript
复制
<?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();
EN

回答 1

Stack Overflow用户

发布于 2019-12-12 08:53:47

尝试下面的代码。唯一的区别是使用terms作为数组。

代码语言:javascript
复制
$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', '' );
    }
}

您可以在这里找到更多信息:https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59300576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档