我试图从一个额外的字段中输出一个列表,我已将其放入自定义帖子中。某些帖子在此字段中包含相同的值,因此在输出列表时我想删除重复项。有谁知道怎么做?
$args = array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'client',
'order' => 'ASC',
'meta_query' => array('relation' => 'AND', array(
'key' => 'client',
'compare' => '='
)
)
);
$query_clients = new WP_Query( $args );
然后我将其放入循环中以从每个帖子中获取元值
$meta = get_post_meta($post->ID,'client', true);
<li><?php echo $meta ?></li>
答案 0 :(得分:0)
我找到了答案。它并不漂亮,但它只是通过检查每个元数据项与前一个元数据项。显然,只有按字母顺序排列时才会有效。
<?php if ($query_clients->have_posts()): while ($query_clients->have_posts()): $query_clients->the_post();
$meta = get_post_meta($post->ID,'client', true); //get the post meta field out of each post
if ($temp_name != $meta){
$temp_name = $meta;
?>
<li><a href="#"><?php echo $meta ?></a></li>
<?php
}
endwhile;
endif;
wp_reset_query();
?>
我希望这有助于某人。