I\'v创建了一个函数来获取一个类别的所有产品,并删除具有相同meta\\u值(CIP)的产品。一切都很好,但如果类别有100多个产品,我的查询速度会非常慢。。。对于2300个产品,查看结果的次数为35“或更多。。。
怎么了?
function get_all_product_in_selected_category($atts){
// Get the category
$catsearched = $atts[\'category_id\'];
// Get all product of categoryselected
$product_args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'fields\' => \'ids\', // Only return product IDs
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'id\',
\'terms\' => $catsearched,
\'operator\' => \'IN\',
))
);
$products = get_posts($product_args);
if(count($products) > 0)
{
// Search CIP by product ID on category
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 from category
$result = array_unique($arrayCip);
//print_r($result);
//echo \'<b>TotalNumberOfUniqueCIP = \'.count($result).\'</b>\';
// Get productID by CIP
foreach ($result as $v)
{
$product = array
(
\'post_type\' => \'product\',
\'posts_per_page\'=> -1,
\'meta_query\' => array
(
array(
\'key\' => \'product_cip\',
\'value\' => $v,
\'compare\'=> \'=\'
)
),
\'fields\' => \'ids\'
);
$product_post = get_posts($product);
//echo \'Uniques CIP = \'.$product_post[0].\', \';
$a[] = $product_post[0];
//echo count($product_post);
}
// If products exist, show products
if(count($a) > 0)
{
$product = array
(
\'post_type\' => \'product\',
\'posts_per_page\'=> -1,
\'meta_key\' => \'product_cip\',
\'post__in\' => $a
);
$allproduct = get_posts($product);
foreach($allproduct as $allp) {
$arrayall[] = $allp->ID;
}
$result = implode(",", $arrayall);
return $result;
}
else
{
return 0;
}
}}
如果我只使用这个:
// Get all product of categoryselected
$product_args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'fields\' => \'ids\', // Only return product IDs
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'id\',
\'terms\' => $catsearched,
\'operator\' => \'IN\',
))
);
$products = get_posts($product_args);
$result = implode(",", $products);
return $result;
时间很快!但我也需要按meta\\u值(CIP)过滤产品
谢谢
编辑:
I\'v修改了我的函数,如下所示:
// Get the category
$catsearched = $atts[\'category_id\'];
// Get all product of categoryselected
$product_args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'fields\' => \'ids\', // Only return product IDs
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'id\',
\'terms\' => $catsearched,
\'operator\' => \'IN\',
))
);
$products = get_posts($product_args);
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><br>\';
$test = array_unique($arrayCip);
//echo \'<b>TotalNumberOfUniqueCIP = \'.count($test).\'</b><br>\';
foreach($test as $id)
{
$array[] = get_post_id_by_meta_key_and_value(\'product_cip\', $id);
}
echo \'<b>TotalNumberOfUniqueID = \'.count($array).\'</b><br>\';
$array = implode(",", $products);
return $array;
并添加了此功能:
function get_post_id_by_meta_key_and_value($key, $value) {
global $wpdb;
$meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->postmeta." WHERE meta_key=%s AND meta_value=%s", $key, $value ) );
if (is_array($meta) && !empty($meta) && isset($meta[0])) {
$meta = $meta[0];
}
if (is_object($meta)) {
return $meta->post_id;
}
else {
return false;
}
}
问题是一样的foreach($test as $id)
{
$array[] = get_post_id_by_meta_key_and_value(\'product_cip\', $id);
}
都很慢!