2017-09-15 146 views
1

我更新WooCommerce到3.0版本,但我不能展示特色產品在我的主題,我用Google搜索了一會兒,並得到WC刪除_Feature和分類添加此。但我不太瞭解我的主題如何獲得特色產品。獲得WooCommerce特色產品中的WP_Query

這是錯誤的特色productcs的代碼。

$meta_query = WC()->query->get_meta_query(); 
    $meta_query[] = array(
     'key' => '_featured', 
     'value' => 'yes' 
    ); 

    $args = array(
     'post_type'   => 'product', 
     'post_status'   => 'publish', 
     'ignore_sticky_posts' => 1, 
     'posts_per_page'  => $products, 
     'orderby'    => $orderby, 
     'order'    => $order == 'asc' ? 'asc' : 'desc', 
     'meta_query'   => $meta_query 
    ); 

如果您知道DataBase中的特色項目在哪裏。非常感謝。

+0

你怎麼想顯示特色產品?你想運行一個'WP_Query'嗎?我無法分辨您的代碼中發生了什麼。你有沒有考慮過使用'[featured_products]'簡碼? – helgatheviking

+0

我如何使用[featured_products]簡碼? –

+0

主題本身爲主題製作了簡碼,這些簡碼就是問題所在。這個提取是代碼的一部分。 –

回答

2

您需要使用tax_query代替:

$tax_query[] = array(
    'taxonomy' => 'product_visibility', 
    'field' => 'name', 
    'terms' => 'featured', 
    'operator' => 'IN', 
); 

// And 

$args = array(
    'post_type'   => 'product', 
    'post_status'   => 'publish', 
    'ignore_sticky_posts' => 1, 
    'posts_per_page'  => $products, 
    'orderby'    => $orderby, 
    'order'    => $order == 'asc' ? 'asc' : 'desc', 
    'tax_query'   => $tax_query 
); 

$query = new WP_Query($args); 

在這裏看到this WooCommerce source code

它應該工作。