获取当前产品类别的所有产品父级 sku 并将它们显示在数组中 - Woocommerce

Get current product category all products parent sku's and display them in array - Woocommerce

我正在尝试获取当前类别中的所有产品 sku。到目前为止,我有这段代码,它只给我 ID。我也尝试过使用 meta_query 但出现了一些错误。我已经对门户网站和其他网站进行了一些研究,但我根本没有得到它。我需要做的是在 all_ids 数组中显示所有当前的 Woocommerce 产品类别产品父 sku。

到目前为止的代码

$all_ids = get_posts( array(
      'post_type' => 'product',
      'numberposts' => -1,
      'post_status' => 'publish',
      'fields' => 'ids',
      'tax_query' => array(
        'relation' => 'AND',
         array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'tenisky',
            'operator' => 'IN',
         )
        ),
        ) );

让我的数据进入我的 HTML 数据层的输出。

<?php echo "'" . implode ( "', '", $all_ids ) . "'"; ?>

提前致谢!

我认为你应该可以使用 get_post_meta:

来解决这个问题
$all_ids = get_posts( array(
      'post_type' => 'product',
      'numberposts' => -1,
      'post_status' => 'publish',
      'fields' => 'ids',
      'tax_query' => array(
        'relation' => 'AND',
         array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'tenisky',
            'operator' => 'IN',
         )
        ),
        ) );

$idsku = []; 

for ($index = 0; $index < count($all_ids); $index++) {
    $idsku[$index] = get_post_meta($all_ids[$index], '_sku', true);
}