100多个产品的查询速度较慢

时间:2018-04-09 作者:ilanb

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); }都很慢!

1 个回复
SO网友:HU ist Sebastian

那么您只希望“第一”个产品具有唯一的product\\u cip?我认为直接MySQL查询可能更快:

function get_all_product_in_selected_category($atts){
// Get the category
if(!(isset($atts[\'category_id\']) || !is_numeric($atts[\'category_id\'])){
   return 0;
}
$catsearched = $atts[\'category_id\'];
global $wpdb;
$productssql = "SELECT min($wpdb->posts.ID), meta_value 
                FROM $wpdb->posts 
                INNER JOIN $wpdb->term_relationships 
                           ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
                INNER JOIN $wpdb->term_taxonomy 
                           ON ($wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id) 
                INNER JOIN $wpdb->postmeta 
                           ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) 
                WHERE 
                           ($wpdb->postmeta.meta_key = \'product_cip\')
                AND ($wpdb->posts.post_type = \'product\') 
                AND ($wpdb->post_status = \'publish\') 
                AND ($wpdb->term_taxonomy.taxonomy = \'product_cat\')
                AND ($wpdb->term_taxonomy.term_id = %d)
                GROUP BY $wpdb->postmeta.meta_value";
$results = $wpdb->get_results($wpdb->prepare($productssql,$catsearched));
$a = array();
if($results){    
     foreach($results as $result){
         $a[] = $result->ID;
     }
     return implode(\',\',$a);
}
return 0;
}
显然没有对此进行测试;)

结束

相关推荐

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

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