我需要从自定义字段数组中获取产品ID,我使用此代码查找具有相同自定义字段的所有产品,删除所有重复的自定义字段值后,我需要检索所有自定义字段的产品ID:
<?php
// Get all product cip by category
$product_args = array(
\'post_type\' => \'product\',
\'no_found_rows\' => true, // Skips SQL to count rows - a speed improvement.
\'post_status\' => \'publish\',
\'ignore_sticky_posts\' => true, // Don\'t move sticky posts to top - a speed improvement.
\'posts_per_page\' => -1,
\'fields\' => \'ids\', // Only return product IDs
);
if (!empty($str_ArrayChild))
{
$product_args[\'tax_query\'] = array(
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'id\',
\'terms\' => $str_ArrayChild,
\'operator\' => \'IN\',
));
}
$products = get_posts($product_args);
// Search CIP by product ID
foreach ( $products as $id )
{
$cip = $product_obj[\'product_cip\']=get_post_meta($id,\'product_cip\');
//echo \'Cip = \'.$cip[0].\', \';
$arrayCip[] = $cip[0];
}
echo \'<b>TotalNumberOfCIP = \'.count($arrayCip).\'</b>\';
// Remove same cip
$result = array_unique($arrayCip);
//print_r($result);
echo \'<b>TotalNumberOfUniqueCIP = \'.count($result).\'</b>\';
// trace Unique Cip list
/*foreach ($arrayCip as $v)
{
echo $v;
}
这里我需要找到
$arrayCip
(名为customfield
product_cip
)
最合适的回答,由SO网友:Dharmishtha Patel 整理而成
$product = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'meta_query\' => array(
array(
\'key\' => \'product_cip\',
\'value\' => \'some value\',
\'compare\' => \'=\',
)
),
\'fields\' => \'ids\',
);
$product_post = get_posts($product);
echo count($product_post);
如果您已获得所有产品数据
$products_array = array();
foreach ($product_post as $v){
$_product = wc_get_product($v);
echo $_product->get_name().\',\';
$products_array[] = $_product;
}
SO网友:ilanb
Ok成立!
foreach ($result as $v)
{
$product=array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\'=> -1,
\'meta_query\' => array(
array( \'key\' =>
\'product_cip\',
\'value\' => $v,
\'compare\'=> \'=\' ) ),
\'fields\' => \'ids\'
);
$product_post=get_posts($product);
//echo $product_post[0].\', \';
$a[] = $product_post[0];
//echo count($product_post);
}
//echo \' ProductID count = \'.count($a).\' \';
// If products exist, show products
if(count($a) > 0)
{
$productArrayForShortcode = implode(",", $a);
echo do_shortcode(\'[products ids="\' . $productArrayForShortcode . \'" per_page="8" columns="4" pagination="true"]\');
}
谢谢你给达米什塔的小费