【问题标题】:Woocommerce search the products by title or product tagsWoocommerce 按标题或产品标签搜索产品
【发布时间】:2020-12-07 21:01:17
【问题描述】:

我使用下面的代码搜索产品,但我只需要按标题或产品标签搜索产品。我该怎么做?

function search_by_title_or_tags( WP_Query $query ): void{
    $search_terms = $query->get( 's' );

    if ( $query->is_search() ) {

        if ( ! $search_terms ) {
             add_action( 'wp', function () use ( $query ) {
             $query->set_404();
             status_header( 404 );
             nocache_headers();
            } );
        }

        $query->set( 'post_type', array( 'post', 'product' )  );
        $query->set( 'posts_per_page', SEARCH_GRID_COUNT_ITEMS );
    }
}

add_action( 'pre_get_posts', 'search_by_title_or_tags' );

【问题讨论】:

    标签: php wordpress woocommerce hook


    【解决方案1】:

    我找到了解决方案。我添加了另一个钩子the_posts,它可以帮助我解决问题。添加以下代码,希望对某人有所帮助:

    function include_tags_in_search( WP_Query $query ): void{
        $search_terms = $query->get( 's' );
    
        if ( $query->is_search() ) {
            global $the_original_paged;
            $the_original_paged = $query->get( 'paged' ) ? $query->get( 'paged' ) : 1;
            if ( ! $search_terms ) {
                 add_action( 'wp', function () use ( $query ) {
                 $query->set_404();
                 status_header( 404 );
                 nocache_headers();
                } );
            }
            $query->set( 'paged', null );
            $query->set( 'post_type', array( 'post', 'product' )  );
            $query->set( 'posts_per_page', SEARCH_GRID_COUNT_ITEMS );
        }
    }
    add_action( 'pre_get_posts', 'include_tags_in_search' );
    
    function add_posts_by_tags( $posts, WP_Query $query ): array {
        if ( $query->is_search() ) {
            global $the_original_paged;
            remove_filter( 'the_posts', 'add_posts_by_tags' );
            $posts_product_cat = new WP_Query( array(
             'posts_per_page' => -1,
             'tax_query' => array(
                  array(
                    'taxonomy' => 'product_tag',
                    'field'    => 'name',
                    'terms'    => explode( ' ', esc_attr( $query->get( 's' ) ) )
                  )
              )
            ) );
            $merged = array_unique( array_merge( $posts, $posts_product_cat->get_posts() ), SORT_REGULAR );
            $posts = array_slice( $merged, ( SEARCH_GRID_COUNT_ITEMS * ( $the_original_paged - 1 ) ), SEARCH_GRID_COUNT_ITEMS );
            $query->found_posts = $posts;
            $query->set( 'paged', $the_original_paged );
            $query->post_count = count( $posts );
            $query->max_num_pages = ceil( count( $merged ) / SEARCH_GRID_COUNT_ITEMS );
            unset( $the_original_paged );
            return $posts;
        }
    
        return $posts;
    }
    add_filter( 'the_posts', 'add_posts_by_tags', 10, 2 );
    

    【讨论】:

    • 你好。在您的代码中,这行:remove_filter( 'the_posts', 'elegance_fly_add_posts_by_tags' ); 有什么作用吗?没有在别处引用的同名函数。
    • @EdA 我编辑了我的答案,它应该是删除add_posts_by_tags 函数。谢谢!
    猜你喜欢
    • 2016-08-07
    • 2018-08-28
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    相关资源
    最近更新 更多