在 WP_Query 中获取 WooCommerce 特色产品 [英] Get WooCommerce featured products in a WP_Query

查看:29
本文介绍了在 WP_Query 中获取 WooCommerce 特色产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 WooCommerce 更新到 3.0 版,但我无法在我的主题中显示特色产品,我在谷歌上搜索了一段时间并让 WC 删除了 _feature 并将其添加到分类法中.但我不太明白我的主题是如何获得特色产品的.

I updated WooCommerce to version 3.0 but I can't show the featured products on my theme, I googled a while and get WC deleted the _feature and add this in taxonomy. But I don't understand so much how my theme get the featured products.

这是错误的特色产品的代码.

Here is the code of the wrong featured 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
    );

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

And if you know where is the featured item in the DataBase. Thanks so much.

推荐答案

从 Woocommerce 3 开始,您需要使用 税务查询 代替,因为 特色产品 现在由product_visibilityfeatured 术语的自定义分类:

Since Woocommerce 3, you need to use a Tax Query instead as featured products are now handled by product_visibility custom taxonomy for the term featured:

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

参考文献:

您可以使用wc_get_featured_product_ids() 函数来获取特色产品 ID 数组 但是在 WP_Query 中使用 税务查询 很好,而且是正确的方法......

You could use wc_get_featured_product_ids() function to get the featured product IDs array but using a tax query in a WP_Query is just fine and the right way…

相关:

它应该有效.

这篇关于在 WP_Query 中获取 WooCommerce 特色产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆