从定制字段数组中查找所有产品ID

时间:2018-01-17 作者:ilanb

我需要从自定义字段数组中获取产品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 (名为customfieldproduct_cip)

2 个回复
最合适的回答,由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"]\');
                            }
谢谢你给达米什塔的小费

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post