2017-09-15 110 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

它应该工作。

相关问题