第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Woocommerce 按標(biāo)題或產(chǎn)品標(biāo)簽搜索產(chǎn)品

Woocommerce 按標(biāo)題或產(chǎn)品標(biāo)簽搜索產(chǎn)品

PHP
素胚勾勒不出你 2023-10-15 17:02:19
我使用下面的代碼來(lái)搜索產(chǎn)品,但我只需要按標(biāo)題或產(chǎn)品標(biāo)簽搜索產(chǎn)品。我怎樣才能做到呢?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' );
查看完整描述

1 回答

?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

我找到了解決方案。我添加了另一個(gè)鉤子the_posts,它幫助我解決了問(wèn)題。添加了以下代碼,希望對(duì)某人有所幫助:


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


查看完整回答
反對(duì) 回復(fù) 2023-10-15
  • 1 回答
  • 0 關(guān)注
  • 180 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)