【问题标题】:Group Woocommerce product by it's category filter by custom taxonomy按自定义分类的类别过滤器对 Woocommerce 产品进行分组
【发布时间】:2018-02-12 18:56:21
【问题描述】:
我的产品具有自定义分类设备,具有 Woocommerce 默认产品类别。
我想按自定义分类在每个产品类别搜索下列出我的产品。
例如:
设备有值:devise-1 和 devise-2
当使用devise-1 搜索时,列表将是:
【问题讨论】:
标签:
wordpress
woocommerce
wordpress-theming
woocommerce-theming
【解决方案1】:
你好,你好吗?我了解到您希望通过自定义类别类型搜索来导出您的帖子。如果是这样,我认为它会有所帮助。对不起,我的英语不好。
对不起,如果我理解错了你的问题。
$your_slug_custom_tax = ( isset($_GET['{your_var_custom_tax}']) ) ? $_GET['{your_var_custom_tax}'] : '';
$your_config = 数组(
'post_type' => 'product', //如果是woocommerce原生
'posts_per_page' => '-1', //您可以根据自己的喜好更改
'post_status' => 'publish', //展示的帖子或产品的状态
'tax_query' => array() // 这是给你的自定义搜索
);
if($your_slug_custom_tax != ''){
$your_config['tax_query'][] = array(
'taxonomy' => '{你的分类学}',
'字段' => '蛞蝓',
'terms' => $your_slug_custom_tax // 展示帖子的分类。
);
}
$your_config = get_posts($your_config);
如果 ( $your_config ) {
foreach ( $your_config 作为 $mypost ):
print_r($mypost);
结束;
}
【解决方案2】:
我找到了一个满足我要求的解决方案,但我认为它很昂贵。因为它是每个类别的查询数据库。
`
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => TRUE,
'taxonomy' => 'product_cat'
);
$product_categories = get_terms($args);
foreach ($product_categories as $product_category) : ?>
<h2 class="title ">
<a href=" <?php echo get_term_link($product_category) ?>"> <?= $product_category->name; // Print Product categories ?> </a>
</h2>
<?php
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_category->slug
),
array(
'taxonomy' => 'device',
'field' => 'slug',
'terms' => $device_type,
'operator' => 'IN'
)
),
'post_type' => 'product',
'orderby' => 'sku,'
);
$products = new WP_Query($args);
// Inner Product loop
while ($products->have_posts()): $products->the_post(); ?>
<?php wc_get_template_part('content', 'product'); ?>
<?php endwhile; ?>
`
任何人都有一个简单的解决方案。谢谢