Wordpress循环 - Meta_Key的唯一值

时间:2013-07-10 09:11:39

标签: php mysql arrays wordpress

我对meta_values进行分组存在问题。 查询使用metakey“company”查找帖子。我想要一个独特的颜色列表,如:蓝红黄

array_unique不成功,也是自定义mysql查询。

<?php
$args = array(
       'category_name' => $cat_name,
       'posts_per_page' => '60',
       'paged' => $current_page,
       'meta_query' => array(
           array(
               'key' => 'company',
               'value' => 'microsoft',
               'compare' => 'like'
           )
        )
     ); 
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>

<?php echo get('color'); ?> 
// Outputs yellow yellow blue yellow red yellow

<?php endwhile; ?>

当前输出为:黄色黄色蓝色黄色,红色,黄色

感谢。

编辑:

感谢您的帮助!!!

这是最终的工作代码:

<?php 
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$cat_name = get_category(get_query_var('cat'))->name;


$args = array(
   'category_name' => $cat_name,
   'posts_per_page' => '60',
   'paged' => $current_page,
   'meta_query' => array(
       array(
           'key' => 'company',
           'value' => 'microsoft',
           'compare' => 'like'
       )
    )
 ); 
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
$colors[] = get('color');

// Creates an array of all colors

endwhile;
$colors = array_unique($colors);
// Removes duplicates;
foreach($colors as $color){
echo $color.' ';
} ?>

1 个答案:

答案 0 :(得分:4)

while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    $colors[] = get('color'); 
    // Creates an array of all colors

endwhile;
$colors = array_unique($colors);
// Removes duplicates;
foreach($colors as $color){
    echo $color;
}