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

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

將 WooCommerce 產(chǎn)品搜索擴(kuò)展到自定義分類法和自定義字段

將 WooCommerce 產(chǎn)品搜索擴(kuò)展到自定義分類法和自定義字段

PHP
慕田峪4524236 2023-10-22 21:28:09
我正在創(chuàng)建高級(jí) woocommerce 搜索,我想在搜索查詢中添加 sku、product_tag 和 Product_category。下面我使用在 WooCommerce 產(chǎn)品搜索答案代碼中啟用自定義分類法,從而啟用多個(gè)分類法的搜索:add_filter( 'posts_search', 'woocommerce_search_product_tag_extended', 999, 2 );function woocommerce_search_product_tag_extended( $search, $query ) {    global $wpdb, $wp;    $qvars = $wp->query_vars;    if ( is_admin() || empty($search) ||  ! ( isset($qvars['s'])    && isset($qvars['post_type']) && ! empty($qvars['s'])    && $qvars['post_type'] === 'product' ) ) {        return $search;    }    // Here set your custom taxonomies in the array    $taxonomies = array('product_tag', 'product_cat');    $tax_query  = array('relation' => 'OR'); // Initializing tax query    // Loop through taxonomies to set the tax query    foreach( $taxonomies as $taxonomy ) {        $tax_query[] = array(            'taxonomy' => $taxonomy,            'field'    => 'name',            'terms'    => esc_attr($qvars['s']),        );    }    // Get the product Ids    $ids = get_posts( array(        'posts_per_page'  => -1,        'post_type'       => 'product',        'post_status'     => 'publish',        'fields'          => 'ids',        'tax_query'       => $tax_query,    ) );    if ( sizeof( $ids ) > 0 ) {        $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $ids ) . ")) OR (", $search);    }    return $search;}我也想在搜索查詢中添加產(chǎn)品 sku,如何添加?
查看完整描述

1 回答

?
HUX布斯

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

add_filter( 'posts_search', 'woocommerce_search_product_tag_extended', 999, 2 );

function woocommerce_search_product_tag_extended( $search, $query ) {

    global $wpdb, $wp;


    $qvars = $wp->query_vars;


    if ( is_admin() || empty($search) ||  ! ( isset($qvars['s'])

    && isset($qvars['post_type']) && ! empty($qvars['s'])

    && $qvars['post_type'] === 'product' ) ) {

        return $search;

    }


    // Here set your custom taxonomies in the array

    $taxonomies = array('product_tag', 'product_cat');


    $tax_query  = array('relation' => 'OR'); // Initializing tax query


    // Loop through taxonomies to set the tax query

    foreach( $taxonomies as $taxonomy ) {

        $tax_query[] = array(

            'taxonomy' => $taxonomy,

            'field'    => 'name',

            'terms'    => esc_attr($qvars['s']),

        );

    }


    // Get the product Ids

    $ids = get_posts( array(

        'posts_per_page'  => -1,

        'post_type'       => 'product',

        'post_status'     => 'publish',

        'fields'          => 'ids',

        'tax_query'       => $tax_query,

    ) );


    if ( sizeof( $ids ) > 0 ) {

        $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $ids ) . ")) OR (", $search);

    }

    return $search;

}

我也想在搜索查詢中添加產(chǎn)品 sku,如何添加?


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

添加回答

舉報(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)