Php 在WP_查询中获取WooCommerce特色产品

Php 在WP_查询中获取WooCommerce特色产品,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我将WooCommerce更新为3.0版,但我无法在我的主题上显示特色产品,我在Google上搜索了一会儿,让WC删除了_功能,并将其添加到分类中。但我不太明白我的主题是如何获得特色产品的 以下是错误功能产品的代码 $meta_query = WC()->query->get_meta_query(); $meta_query[] = array( 'key' => '_featured', 'value' => 'yes'

我将WooCommerce更新为3.0版,但我无法在我的主题上显示特色产品,我在Google上搜索了一会儿,让WC删除了_功能,并将其添加到分类中。但我不太明白我的主题是如何获得特色产品的

以下是错误功能产品的代码

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

如果你知道数据库中的特色项目在哪里。非常感谢。

自Woocommerce 3以来,您需要使用税务查询,因为特色产品现在由
产品可见性
自定义分类法处理
特色

// The tax query
$tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN', // or 'NOT IN' to exclude feature products
);

// The query
$query = new WP_Query( 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 // <===
) );
//税务查询
$tax_query[]=数组(
“分类法”=>“产品可视性”,
'字段'=>'名称',
“术语”=>“特色”,
“运算符”=>“在”、//或“不在”以排除功能产品
);
//询问
$query=新的WP\U查询(数组(
“post_类型”=>“产品”,
“发布状态”=>“发布”,
“忽略粘性帖子”=>1,
“每页发布”=>$products,
'orderby'=>$orderby,
'order'=>$order=='asc'?'asc':'desc',

“tax\u query”=>$tax\u query/这是一个老问题,但您也可以使用:


刚刚在这里发现它。我希望它能有所帮助!

您现在可以使用参数featured设置为true的wc\u get\u产品。请参阅

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 12,
        'tax_query' => array(
                array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'name',
                    'terms'    => 'featured',
                ),
            ),
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();

对于希望按类别获取特色产品的用户,您可以查看我的注释=>

您如何尝试显示特色产品?您是否尝试运行
WP\u查询
?我不知道您的代码中发生了什么。您是否考虑过使用
[特色产品]
快捷码?我如何使用[featured_products]shortcode?主题本身为主题创建了短代码,而这些短代码就是问题所在。此摘录是代码的一部分。谢谢,它工作得很好!我不明白为什么它们从Posteta转移到分类法,毫无意义。
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 12,
        'tax_query' => array(
                array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'name',
                    'terms'    => 'featured',
                ),
            ),
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();
$args = array(
    'featured' => true,
);
$products = wc_get_products( $args );