【问题标题】:Custom posts with multiple terms only being returned once具有多个术语的自定义帖子仅返回一次
【发布时间】:2026-01-19 22:00:01
【问题描述】:

我有一个自定义帖子类型 products,它的分类是 range。 我有另一个自定义帖子类型 stockist,它使用 ACF post_object 将产品与库存商相关联。

我正在尝试遍历库存商帖子,返回每个术语的标题,然后通过帖子对象返回与库存商关联的每个产品的列表。

<?php                           
$args2 = array( 'post_type' => 'stockist', 'posts_per_page' => -1 );
$stockistloop2 = new WP_Query( $args2 );
if ( $stockistloop2->have_posts() ): while ( $stockistloop2->have_posts() ): $stockistloop2->the_post();?>
<div class="col-1-1 clearfix nopad stockist-block-dropdown STORE <?php the_title();?>">
    <h2 class="stockist-title"><?php the_title();?></h2>
<?php 
$args = array( 'taxonomy' => 'range');
$categories = get_categories($args);
 if($categories): foreach($categories as $category): $url = get_category_link( $category->term_id ); ?>
    <div class="col-1-5 mobile-col-1-2">
        <h4><?php echo ($category->name) ;?></h4>
        <?php $post_objects = get_field('stocked_range'); if( $post_objects ): ?>
        <ul class="stockist-block-products clearfix">
        <?php foreach( $post_objects as $post_object): $post_terms_array = get_the_terms($post_object, 'range'); $post_term_name = $post_terms_array[0]->slug; 
            if($post_term_name == $category->slug):?>
            <li>
                <a href="<?php echo get_permalink($post_object->ID); ?>" target="_blank"><?php echo get_the_title($post_object->ID); ?></a>
            </li>
            <?php endif; endforeach; ?>
        </ul>
    <?php  endif;?>
    </div>
    <?php endforeach; endif;?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>

这在某种程度上有效,但产品有多个术语。例如,pork sausage 可能有术语“sausages”和“bbq”,它只在 sausages 下返回一次,而不是在两个术语下都返回。

任何人都可以提供任何建议吗?

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    您的问题在于以下几行:

    $post_term_name = $post_terms_array[0]->slug;
    if($post_term_name == $category->slug):
    

    您将循环中的每个 $category 与仅与产品关联的第一个术语进行比较。试试这个(为了我自己的清楚,我稍微编辑了格式)。

    <?php                           
    $args2 = array( 
        'post_type'      => 'stockist', 
        'posts_per_page' => -1 
    );
    $stockistloop2 = new WP_Query( $args2 );
    if ( $stockistloop2->have_posts() ): while ( $stockistloop2->have_posts() ): $stockistloop2->the_post();
    ?>
    <div class="col-1-1 clearfix nopad stockist-block-dropdown STORE <?php the_title();?>">
        <h2 class="stockist-title"><?php the_title();?></h2>
        <?php 
        $args = array( 
            'taxonomy' => 'range'
        );
        $categories = get_categories($args);
        if($categories): 
        foreach($categories as $category): 
        $url = get_category_link( $category->term_id ); 
        ?>
        <div class="col-1-5 mobile-col-1-2">
            <h4><?php echo ($category->name); ?></h4>
            <?php 
            $post_objects = get_field('stocked_range'); 
            if( $post_objects ): 
            ?>
            <ul class="stockist-block-products clearfix">
                <?php 
                foreach( $post_objects as $post_object ): 
                $post_terms_array = get_the_terms($post_object, 'range'); 
                $post_terms_id_array = wp_list_pluck( $post_terms_array, 'term_id' );
                if( in_array( $category->term_id, $post_terms_id_array ) ):
                ?>
                <li>
                    <a href="<?php echo get_permalink($post_object->ID); ?>" target="_blank"><?php echo get_the_title($post_object->ID); ?></a>
                </li>
                <?php 
                endif;
                endforeach; 
                ?>
            </ul>
            <?php endif;?>
        </div>
        <?php 
        endforeach; 
        endif;
        ?>
    </div>
    <?php 
    endwhile; 
    wp_reset_postdata(); 
    endif; 
    ?>
    

    我正在创建一个仅包含术语 ID 的数组,然后检查当前的 $category ID 是否在该数组中。

    【讨论】:

    • 感谢 johnnyd23。通过代码,我意识到那是我出错的地方,但我花了几个小时才想出一个解决方案!感谢您的帮助。
    最近更新 更多