dongqu4443 2014-11-15 03:29
浏览 167
已采纳

WooCommerce:返回特定类别中所有产品ID的函数

I think the title of the question is fairly self-explanatory, but to detail it...

I'm trying to make a custom category page in Woocommerce.

What I need is to return all the product ID's in a particular category.

I've seen this post, but it uses the WP_Query way of doing things, which is pretty ugly.

I'd prefer to use something in the WooCommerce classes to accomplish this.

Ideally, there should some sort of function/method like the following (but I can't find it):

get_products_in_category( $category_ID );
// Returns array of product ID's

Any help would be amazing.

  • 写回答

1条回答 默认 最新

  • dpi10335 2014-11-15 03:44
    关注

    Here you are:

    function get_products_from_category_by_ID( $category_id ) {
    
        $products_IDs = new WP_Query( array(
            'post_type' => 'product',
            'post_status' => 'publish',
            'fields' => 'ids', 
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => $category_id,
                    'operator' => 'IN',
                )
            )
        ) );
    
        return $products_IDs;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题