从特定post_type中排除帖子

问题描述 投票:0回答:2

我创建了一个post_type“集合”,它的所有分类都与post_type“product”相关联

所以当我这样做

global $wp;
$posts = get_terms( $wp->query_vars["name"]);
foreach($posts as $post): ?>
<a href="<?php echo get_term_link( $post );?>" rel="bookmark"><?=$post->name . " (".$post->count.")" ?> </a>
<?php endforeach;

这将在分类“风格”下显示post_type“product”中的所有术语

但它也显示了分类“风格”下的post_type“集合”下的术语

如何在post_type“collection”下排除显示条款

enter image description here

php wordpress woocommerce custom-post-type taxonomy
2个回答
0
投票

弗朗西斯,代码目前提取“条款”而非帖子,这就是为什么你得到的比你想要的更多。如果您想要获取帖子(而不是条款),请使用https://developer.wordpress.org/reference/functions/get_posts/

这个例子最接近你正在寻找的https://developer.wordpress.org/reference/functions/get_posts/#comment-2516,替换你的post_type和分类。


0
投票

我有点这样做了

<?php
$posts = get_terms($wp->query_vars["name"]);
$terms = get_terms($wp->query_vars["name"], array(
    'hide_empty' => 0,
));

foreach( $terms as $term ) :
    wp_reset_query();
    $args = array('post_type' => 'product',
        'tax_query' => array(
            array(
                'taxonomy' => $wp->query_vars["name"],
                'field' => 'slug',
                'terms' => $term->slug          
            ),
        ),
     );
    $posts = new WP_Query($args); 

    if( $posts->have_posts() ) : ?>
        <h3><?php echo $term->name; ?> - <?php echo $posts->post_count ?></h3>
    <?php endif; endforeach; ?>

所以$ posts-> post_count就可以了

另一个问题是它隐藏在空的时候,我需要显示所有空的

© www.soinside.com 2019 - 2024. All rights reserved.